2010-07-07 70 views

回答

2

作为WordPress的3.0的,用于选择一个模板中wp-includes/template-loader.php逻辑看起来像这样:

if (defined('WP_USE_THEMES') && WP_USE_THEMES) : 
    $template = false; 
    if  (is_404()   && $template = get_404_template()   ) : 
    elseif (is_search()   && $template = get_search_template()  ) : 
    elseif (is_tax()   && $template = get_taxonomy_template()  ) : 
    elseif (is_front_page()  && $template = get_front_page_template() ) : 
    elseif (is_home()   && $template = get_home_template()   ) : 
    elseif (is_attachment()  && $template = get_attachment_template() ) : 
     remove_filter('the_content', 'prepend_attachment'); 
    elseif (is_single()   && $template = get_single_template()  ) : 
    elseif (is_page()   && $template = get_page_template()   ) : 
    elseif (is_category()  && $template = get_category_template()  ) : 
    elseif (is_tag()   && $template = get_tag_template()   ) : 
    elseif (is_author()   && $template = get_author_template()  ) : 
    elseif (is_date()   && $template = get_date_template()   ) : 
    elseif (is_archive()  && $template = get_archive_template()  ) : 
    elseif (is_comments_popup() && $template = get_comments_popup_template()) : 
    elseif (is_paged()   && $template = get_paged_template()   ) : 
    else : 
     $template = get_index_template(); 
    endif; 
    if ($template = apply_filters('template_include', $template)) 
     include($template); 
    return; 
endif; 

检查在get_category_template() WP-包括/ theme.php`我们看到:

function get_category_template() { 
    $cat_ID = absint(get_query_var('cat')); 
    $category = get_category($cat_ID); 

    $templates = array(); 

    if (!is_wp_error($category)) 
     $templates[] = "category-{$category->slug}.php"; 

    $templates[] = "category-$cat_ID.php"; 
    $templates[] = "category.php"; 

    $template = locate_template($templates); 
    return apply_filters('category_template', $template); 
} 

假设您的类别为Foo,它的子弹为foo,并且Foo类别ID为17,对于属于类别的帖子10,WordPress会检查你的主题下面的模板,并使用它找到的第一个:

  • 类别的foo.php
  • 类别的17.php
  • category.php

因此,您所需要做的就是在您的主题目录中创建一个名为category-foo.php的模板,并将您的帖子的类别设置为Foo,该帖子将使用category-foo.php模板而不是默认post.php模板呈现。

自从Wordpress 1.5以来,这种用于选择模板的机制已经存在,尽管多年来模板类型的完整列表已经显着增长。

关于此的Wordpress文档可以在here找到。

+0

我的固定链接设置为ID。因此,每个类别都与它的ID一起引用。我已经为id3为类别3.php的类别“real-estate”做了一个类别文件,但它不适用于类别为“real-estate”的帖子。 – 2010-07-07 09:41:24

+0

永久链接格式没有任何内容与选择模板的机制有关。我已经给你在哪里寻找调试,但你将不得不做的外观,因为我不知道(a)你使用的是什么版本的WordPress,(b)你是什么主题使用,(c)你安装了什么插件,或其他什么。如果我不得不猜测,您的帖子类别ID不是真的3 - 将模板重命名为category-real-estate.php,并确保它可以被网络服务器读取。 – 2010-07-07 09:54:28

+0

类别ID是3。无论如何,如果这些帖子应该有父类别的模板,并且没有别的东西,那么我会看到它。 但请告诉我,会发生多个类别的帖子会发生什么? – 2010-07-07 16:42:49

相关问题