wordpress网站如何修改文章类型页面模板

 火.. [复制链接]  928查看
易西 发表于 2023-9-9 19:47:20 | 显示全部楼层 |阅读模式
wordpress网站如何修改文章类型页面模板
[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 4 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2023-9-9 19:49:49 | 显示全部楼层
https://www.ebingou.cn/dmh/4915.html
参考

如果想让某个分类的文章页面样式有别于其它分类,我们可以使用自定义的模板的方法实现。例如,我们准备让名称为WordPress的分类文章使用有别于其它分类的模板样式,

首先在所用主题根目录新建一个名称 single-wordpress.php的模板文件。将以下代码片段添加到您的当前主题的 functions.php 文件:

  1. add_action('template_include', 'load_single_template');
  2. function load_single_template($template) {
  3.      $new_template = '';
  4.     // single post template   
  5.     if( is_single() ) {      
  6.     global $post;
  7.      // 'wordpress' is category slugs      
  8.      if( has_term('wordpress', 'category', $post) ) {        
  9.      // use template file single-wordpress.php        
  10.      $new_template = locate_template(array('single-wordpress.php' ));
  11.            }
  12.           }   
  13.            return ('' != $new_template) ? $new_template : $template;  
  14.            }
复制代码

 楼主| 易西 发表于 2023-9-9 20:11:46 | 显示全部楼层
本帖最后由 易西 于 2023-9-9 20:13 编辑

https://xiaoqingtai.com/3069.html
参考

文章和自定义文章类型的自定义模板
从WordPress 4.7开始,有一个新的鲜为人知的功能,可以让你为文章、页面和自定义文章类型添加多个模板。

要测试此新功能,我们首先必须注册一个自定义文章类型。将以下代码复制到你主题的functions.php文件中,来创建一个新的自定义文章类型,称为“Product”

  1. //注册一个自定义文章类型
  2. function create_product_post_type() {
  3. register_post_type( 'product',
  4. array(
  5. 'labels' => array(
  6. 'name' => __( 'Products' ),
  7. 'singular_name' => __( 'Product' )
  8. ),
  9. 'public' => true,
  10. 'has_archive' => true,
  11. 'show_in_rest' => true // 启用古腾堡编辑器
  12. )
  13. );
  14. }
  15. add_action( 'init', 'create_product_post_type' );
复制代码

创建自定义文章类型时,有很多选项可以使用,但出于我们的目的,我们将尽可以简单。记下第12行。在这里您可以在Gutenberg和经典编辑器之间切换。在编写本文时,默认情况下,自定义文章类型将启用经典编辑器,可以通过添加第12行的代码,来启用Gutenberg编辑器。

创建文章和自定义文章类型的模板
要为我们的新的文章类型 Product 创建模板,我们可以创建一个product.php文件并为其添加布局。很好,但是如果我们需要特定产品的不同布局怎么办?好吧……就像我说的那样……从WordPress 4.7开始,你可以在模板文件中使用新的特殊注释标题。

在主题目录中创建一个名为 full-width-page-layout.php 的新文件,并在顶部添加以下代码片段。

  1. <?php
  2. /*
  3. Template Name: Full-width page layout
  4. Template Post Type: post, page, product
  5. */
复制代码

这样就注册了新模板,用于文章、页面和自定义文章类型 Product。它将在经典和古腾堡编辑器的编辑页面右侧显示“发布属性”框,如下所示。


请注意,你可以将“Template Post Type:”的值更改为任何已注册的文章类型。因此,如果只希望将模板应用于 Product 文章类型,请更改标题,如下所示:

  1. <?php
  2. /*
  3. Template Name: Full-width page layout
  4. Template Post Type: product
  5. */
复制代码
[发帖际遇]: 易西 捡了钱没交公 威望 降了 3 . 幸运榜 / 衰神榜
 楼主| 易西 发表于 2023-9-9 20:34:56 | 显示全部楼层
本帖最后由 易西 于 2023-9-9 20:45 编辑

https://www.yii666.com/blog/423269.html
参考

本文实例讲述了WordPress使用自定义文章类型实现任意模板的方法。分享给大家供大家参考,具体如下:

这几天在搭建博客的时候,碰到了一个对我来说很棘手的问题。WordPress自带的文章类型只能使用他们特定的模版,而我由于想做一个心情时间轴的板块,所以只能自定义文章的类型,让他来支持我的这个模版。于是网上找资料,并且以插件的形式来表现,得到了如下的解决方案:

主要就是使用了register_post_type 函数。网址:yii666.com<

1、创建插件目录

新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood文章来源地址https://www.yii666.com/blog/423269.html文章地址https://www.yii666.com/blog/423269.html

2、创建PHP代码文件

在刚才创建的文件夹里面新建一个php文件,命名为myMood,用来书写插件代码

3、添加头部描述


复制代码
代码如下:
  1. <?php
  2. /*
  3. Plugin Name: Movie Reviews
  4. Plugin URI: <a href="http://wp.tutsplus.com/" target="_blank">http://wp.tutsplus.com/</a>
  5. Description: Declares a plugin that will create a new post type .
  6. Version: 1.0
  7. Author: Summer
  8. Author URI: <a href="http://www.xtwind.com/" target="_blank">http://www.xtwind.com/</a>
  9. License: GPLv2
  10. */
  11. ?>
复制代码


4、注册自定义函数
在刚刚创建的php文件代码中,在?>前面添加函数:


复制代码
代码如下:
  1. add_action( 'init', 'create_myMood' );
复制代码


得到如下代码:

复制代码
代码如下:
  1. <?php
  2. /*
  3. Plugin Name: Movie Reviews
  4. Plugin URI: <a href="http://wp.tutsplus.com/" target="_blank">http://wp.tutsplus.com/</a>
  5. Description: Declares a plugin that will create a new post type .
  6. Version: 1.0
  7. Author: Summer
  8. Author URI: <a href="http://www.xtwind.com/" target="_blank">http://www.xtwind.com/</a>
  9. License: GPLv2
  10. */
  11. add_action( 'init', 'create_myMood' );
  12. ?>

复制代码

5、添加函数功能
把下面这段代码添加到 add_action( 'init', 'create_myMood' ); 的前面


复制代码
  1. function create_lsxq() {
  2. register_post_type( 'lsxq',
  3. array(
  4. 'labels' => array(
  5. 'name' => '零散心情',
  6. 'singular_name' => 'lsxq',
  7. 'add_new' => '写心情',
  8. 'add_new_item' => '添加一条新心情',
  9. 'edit' => 'Edit',
  10. 'edit_item' => 'Edit lsxq',
  11. 'new_item' => 'New lsxq',
  12. 'view' => 'View',
  13. 'view_item' => 'View lsxq',
  14. 'search_items' => 'Search lsxq',
  15. 'not_found' => 'No lsxq found',
  16. 'not_found_in_trash' => 'No lsxq found in Trash',
  17. 'parent' => 'Parent lsxq'
  18. ),
  19. 'public' => true,
  20. 'menu_position' => 15,
  21. 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
  22. 'taxonomies' => array( '' ),
  23. 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
  24. 'has_archive' => true
  25. )
  26. );
  27. }
复制代码

对 register_post_type 这个函数发出声明,它就为新的文章类型做好了各种管理功能。这个函数包括两个参数:第一个是定义了自定义文章类型的名字 ;第二个是一个数组,用来定义新的自定义文章类型的属性。

第一个参数很简单,大家自己领悟。这里简单说下地位个参数:

'public' => true 决定该文章类型在管理后台和前端的可见性
'menu_position' => 5 决定该文章类型菜单的位置
'supports' => array( 'title', 'editor', 'comments', 'thumbnail') 决定自定义文章类型的功能
'taxonomies' => array( '' ) 创建自定义分类,这里没有定义。
'menu_icon' => plugins_url( 'image.png', __FILE__ ) 显示管理菜单的图标,图标文件放在和插件同一目录,为16*16像素
'has_archive' => true 启用自定义文章类型的存档功能

请访问 register_post_type 了解更多关于该函数的参数细节。

6、创建一个该自定义文章类型的模版
打开刚刚的代码文件,在

复制代码
代码如下:
  1. add_action( 'init', 'create_lsxq' );
复制代码

语句前面添加下面这一语句:

复制代码
代码如下:
  1. add_filter( 'template_include', 'include_template_function', 1 );
复制代码


7、实现该函数的功能

复制代码
代码如下:
  1. function include_template_function( $template_path ) {
  2. if ( get_post_type() == 'lsxq' ) {
  3. if ( is_single() ) {
  4. if ( $theme_file = locate_template( array ( 'single-lsxq.php' ) ) ) {
  5. $template_path = $theme_file;
  6. } else {
  7. $template_path = plugin_dir_path( __FILE__ ) . '/single-lsxq.php';
  8. }
  9. }
  10. }
  11. return $template_path;
  12. }
复制代码


该代码段添加在下面语句的后面

复制代码
代码如下:
  1. add_filter( 'template_include', 'include_template_function', 1 );
复制代码

8、创建单页面模版single-lsxq.php网址:yii666.com文章来源地址:https://www.yii666.com/blog/423269.html

创建一个名字为single-lsqx.php的文件,主要代码段如下:


复制代码
代码如下:
  1. <?php
  2. $mypost = array( 'post_type' => 'lsxq', );
  3. $loop = new WP_Query( $mypost );
  4. ?>
  5. <?php while ( $loop->have_posts() ) : $loop->the_post();?>
  6. <div class="item">
  7. <h3> <span class="fr"><?php the_time('Y-n-j H:i') ?></span> </h3>
  8. <div class="con">
  9. <?php the_content(); ?>
  10. </div>
  11. </div>
  12. <?php endwhile; ?>
复制代码


现在,我们已经通过循环创建了一个基本的页面模板。这个  函数检索自定义文章类型的元素,并在循环中使用它们。现在自定义文章类型差不多就好了,剩下的就是css样式了。
上述代码可点击此处本站下载。

希望本文所述对大家基于wordpress的网站建设有所帮助。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

邮箱|首页|小黑屋|吾侪 ( 蜀ICP备2020029307号-4 )

GMT+8, 2025-12-1 20:56 , Processed in 0.057267 second(s), 22 queries , APCu On.

Powered by Discuz! X3.5

Copyright © , 吾侪网

快速回复 返回顶部 返回列表