wordpress不同分类调用不同模板文件
用wordpress做站特别是企业站的时候,多个分类的内容不同需要显示的页面也不一样,比如有些分类显示的是产品缩略图,有些显示的是公司新闻公告等,此时需要不同的分类模板,虽然wordpress默认只有一个通用的分类模板,但是可以通过函数代码实现不同的分类调用不同的模板。
实现WordPress不同分类调用不同模板的方法:
在当前WordPress主题的category.php文件中(没有就新建一个),添加以下判断代码:
- $post = $wp_query->post;
- if(in_category('1')) {
- include(TEMPLATEPATH.'/category-1.php');
- }
- else if (in_category('2')){
- include(TEMPLATEPATH./'category-2.php');
- }
- else {
- include(TEMPLATEPATH.'/category-3.php');
- }
- ?>
该判断函数表示:判断分类ID是否为‘1’,如果是,则调用category-1.php模板,如果分类ID是2则调用category-2.php模板,如果以上两者都不是则调用category-2.php模板
提醒:也可以用类似方法实现不同文章调用不同页面,不同归档调用不同页面等
在制作wordpress多栏目模板时,遇到不同的分类调用不同模板的情况,假如我的博客有分类PHP技术,ASP技术,每个分类需要调用不同的分类模板,发现一个简单的调用方法,下面就让夏日博客教大家如何调用不同的分类吧。
一、不同分类调用不同模板
- <?php
- $post = $wp_query->post;
- if ( in_category(‘7′) )
- {
- include(TEMPLATEPATH . ‘/archive-view.php’); }
- else if ( in_category(‘12′) )
- {
- include(TEMPLATEPATH . ‘/single12.php’);
- }
- else if ( in_category(‘42′) )
- {
- include(TEMPLATEPATH . ‘/single42.php’);
- }
- else { include(TEMPLATEPATH . ‘/archive-other.php’);
- }
- ?>
上面的代码是摘自网络上的一段代码,将代码放入到主题的分类文件archive.php中就可以了,in_category(’7′) 中间的数字代码的是分类的ID,这个在后台分类目录中可以看到,而 archive-view.php 则是模板文件,需要在模板里面新建 single-view.php 文件才可以调用。
二、不同文章按照分类来调用不同模板
在不同的分类中调用了不同的模板,哪么分类下面的文章也需要调用当前分类的模板,看下下面的实现方法,
- <?php
- $post = $wp_query->post;
- if ( in_category(‘7′) ) {
- include(TEMPLATEPATH . ‘/single-view.php’);
- }
- else if ( in_category(‘3′))
- {
- include(TEMPLATEPATH . ‘/single-case.php’);
- }
- else if ( in_category(‘42′) )
- {
- include(TEMPLATEPATH . ‘/single42.php’);
- }
- else { include(TEMPLATEPATH . ‘/archive-other.php’);
- }
- ?>
和 wordpress 不同分类调用不同模板的文件基本一样,就是调用的模板文件 single-view.php 有所不同,这个也同样是需要新建的,这样才可以调用到。当然这段代码也是要放到文章模板页 single.php 中的。
三、首页