wp教程:15WordPress不同分类下的文章调用不同模板

WordPress不同分类下的文章调用不同模板

1 get_template_part() 自定义模板文件调用函数
2 get_the_category() 获取当前文章的分类
3 get_the_ID() 获取当前文章ID,同 $post->ID

1.如何使用 get_template_part 动态调用内容模板

<?php get_template_part(‘内容模板’,’分类目录别名’); ?>

2.如何获取当前文章的分类信息

<?php
$cat=get_the_category(get_the_ID());
$name=$cat[0]=>slug;
echo $name;
get_template_part(‘内容模板’,$name);
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *