2016-09-21 28 views

回答

0

我已經結束了以下柺杖。

推杆以下到functions.php中檢測電流後的頂層父類ID:

function get_top_level_cat_id ($catid) { 
while ($catid) { 
$cat = get_category($catid); 
$catid = $cat->category_parent; 
$catParent = $cat->cat_ID; 
} 
return $catParent; 
} 

然後複製當前的single.php到_single.php他們更換了所有的single.php內容有:

<?php 
// functions.php !!! 
$category = get_the_category($post->ID); 
$catid = $category[0]->cat_ID; 
$top_level_cat_id = get_top_level_cat_id ($catid); 

if ($top_level_cat_id == XX) {require __DIR__ . '/_single_XXX.php';} 
elseif ($top_level_cat_id == YY) {require __DIR__ . '/_single_YYY.php';} 
else {require __DIR__ . '/_single.php';} 
?> 

所以,現在我可以將_single.php複製到_single_XXX.php中,並對其進行編輯。同樣是_single_YYY.php,_single_ZZZ.php等。

我仍然不滿意這樣的解決方案,並會感謝任何更好的建議。

0

您可以在WordPress.org網站上使用該信息。

這是我發現做一個快速Google Search

時,基本上你有這些模板文件(這些也都是在什麼WordPress會尋找第一順序):

1)category-slug.php
2) category-ID.php
3)category.php

+0

那些將增加自定義模板針對特定類別的網頁,而我正在尋找分配一個模板文件的方法**在給定類別內的所有帖子**,即使沒有觸及類別頁面本身。 – YKKY

0

你可以在使用的single.php這個代碼轉換模板,像這樣:

if (in_category('fruit')) { 
     include 'single-fruit.php'; 
    } elseif (in_category('vegetables')) { 
     include 'single-vegetables.php'; 
    } else { 
     // Continu enter code here with normal Loop 
     if (have_posts()) : while (have_posts()) : the_post(); 
     // ... 
    } 
相關問題