请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

WordPress自定义文章页面模板

[复制链接]
查看3738 | 回复0 | 2022-1-16 19:26:33 | 显示全部楼层 |阅读模式
论坛免责声明
在这里购买好电脑https://xrkj.taobao.com/

如果想让某个分类的文章页面样式有别于其它分类,我们可以使用自定义的模板的方法实现。例如,我们准备让名称为 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.   }
复制代码

上面的代码将指定 WordPress 分类的文章,使用 single-wordpress.php 模板文件。同理,你可以重复以上的步骤,让其它分类也可以使用自定义模板。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
论坛免责声明
最新常见问题解决方案 (出处: 星瑞科技)
回复

使用道具 举报

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

本版积分规则