分享学习记录
互联网技术知识

WordPress自定义文章页面模板的教程

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

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

add_action('template_include', 'load_single_template');
  function load_single_template($template) {
    $new_template = '';
    // single post template
    if( is_single() ) {
      global $post;
      // 'wordpress' is category slugs
      if( has_term('wordpress', 'category', $post) ) {
        // use template file single-wordpress.php
        $new_template = locate_template(array('single-wordpress.php' ));
      }
    }
    return ('' != $new_template) ? $new_template : $template;
  }

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

赞(0)
文章名称:《WordPress自定义文章页面模板的教程》
文章链接:https://www.bailuze.com/9861.html
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站专注于百度、搜狗、360、谷歌、bing等常见搜索引擎的优化,关键词排名的提高,诚意咨询邮箱526009505@qq.com
分享到