2010-07-07 28 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