近日,因为网站建设的需要,在没有使用自定义文章类型的情况下,使用不同的分类目录里的文章调用不同的模板,作为注册弗洛鱼的见面礼。

首先在function.php里,添加如下代码:

//获取并输入某个分类的子分类
function post_is_in_descendant_category( $cats, $_post = null )
{
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}

复制一份single.php,命名为:single-*.php文件名(你可以根据自己的需要,制作多个 single-*.php 文件,通过修改每个single-*.php 文件的html结构和添加对应的CSS,就可以实现不同的文章页面样式 )。

将 single.php 里面除了 get_header(); get_footer(); get_sidebar(); 之外的所有内容改成:

注:该文章的方法就是使用 in_category() 函数来判断,不添加了自定义函数 post_is_in_descendant_category() 来获取某个分类的子分类。

由 floryu 发布

喜欢折腾,喜欢分享,喜欢WordPress,提供WordPress主题、插件汉化,以及WordPress、DEDECMS建站。

发表回复