2016-07-25 136 views
0

我有多個標籤,並且想要爲每個人標籤創建多個single.php。通過標籤ID或名稱爲特定標籤創建single.php

如何爲標籤創建single.php?

此代碼適用於類別,如何編輯標籤?

function my_category_templates($single_template) { 
global $post; 

if (in_category('raspee')) { 
    $single_template = dirname(__FILE__) . '/single-raspee.php'; 
} 
    return $single_template; 
} 
add_filter("single_template", "my_category_templates"); 
+0

在此發佈您的問題http://wordpress.stackexchange.com/ – unixmiah

+0

您應該瞭解[Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/#tag)。 – MinhTri

回答

1

您可以檢查標籤與has_tag()功能...

if (has_tag('awesome')) { 
    $single_template = dirname(__FILE__) . '/single-awesome.php'; 
} 

你整個代碼看起來就像這樣......

function my_category_templates($single_template) { 
global $post; 

    if (in_category('raspee')) { 
    $single_template = dirname(__FILE__) . '/single-raspee.php'; 
    } else if (has_tag('awesome')) { 
    $single_template = dirname(__FILE__) . '/single-awesome.php'; 
    } 
    return $single_template; 
} 
add_filter("single_template", "my_category_templates"); 

我們增加else if以測試只有在第一個條件不符合的情況下才會標記它,如果您必須再次使用if ...
希望有所幫助。