在使用WordPress建设网站的时候,如果是博客类型的话,结构还是比较简单的,基本栏目都是同一类型的布局,如果开发复杂一点的网站,可能就要求不同类型的内容不同的展示方式了,其实,这也很简单,无非就是多个判断而已,然后,调用不同结构模板。
首先在function.php里,添加如下代码:
//获取并输入某个分类的子分类
1 2 3 4 5 6 7 8 9 10 | 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(); 之外的所有内容改成:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if ( in_category('16') || post_is_in_descendant_category(16) )//可自行修改 这里包含分类目录里的文章和分类目录里的子分类目录里的文章 { include(TEMPLATEPATH .'/single-16.php'); } elseif ( in_category('7') || post_is_in_descendant_category(7) )//如果只有两类single.php,可以不要这段,如果是多类,则添加多个elseif { include(TEMPLATEPATH . '/single-7.php'); } else{ include(TEMPLATEPATH . '/single-other.php'); }//给其他分类的文章调用的。 ?> |
上面就是WordPress不同分类目录调用不同的文章模板的方法,是不是很简单?你学会了吗?
转载请保留本文链接:https://www.zhe94.com/127.html