欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress如何自定义文章详情页模板

作者 : 自学哈 本文共633个字,预计阅读时间需要2分钟 2022-11-26 共91人阅读
也想出现在这里? 联系我们

如果想让某个分类的文章页面样式有别于其它分类,我们可以使用自定义的模板的方法实现。例如,我们准备让名称为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 模板文件。同理,你可以重复以上的步骤,让其它分类也可以使用自定义模板。

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress如何自定义文章详情页模板
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号