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 模板文件。同理,你可以重复以上的步骤,让其它分类也可以使用自定义模板。

以上就是WordPress如何自定义文章详情页模板的详细内容,更多请关注网站的其它相关文章!

WordPress如何自定义文章详情页模板 (https://www.wp-admin.cn/) WordPress使用教程 第1张

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-11 05:51:19
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: