Php Wordpress为子类别和文章使用自定义模板
有一个大的通用类别Php Wordpress为子类别和文章使用自定义模板,php,wordpress,Php,Wordpress,有一个大的通用类别目录($category->cat_ID=2)。它包含一些子类别,如系列1,系列2等。每个子类别都包含一些子类别,如Type1,Type2,等等。每个子类别都包含一些产品(typepost)。 我不需要显示普通类别目录及其子类别,但我需要在单独页面上显示其产品的子类别。 如何为categoryCatalog及其子类别设置404.php模板,以及为每个子类别(带产品列表)和每个产品设置自定义模板 我在下面找到了代码(functions.phpfile) 但我不知道如何根据我的需要
目录($category->cat_ID=2
)。它包含一些子类别,如系列1
,系列2
等。每个子类别都包含一些子类别,如Type1
,Type2
,等等。每个子类别都包含一些产品(typepost
)。
我不需要显示普通类别目录及其子类别,但我需要在单独页面上显示其产品的子类别。
如何为categoryCatalog
及其子类别设置404.php
模板,以及为每个子类别(带产品列表)和每个产品设置自定义模板
我在下面找到了代码(functions.php
file)
但我不知道如何根据我的需要定制它
UPD.我已经编写了下面的代码
function catalog_template() {
// Get the category id from global query variables
$cat = get_query_var('cat');
if(!empty($cat)) {
$catalog_id = get_cat_ID('Catalog');
$catalog_child_cats = array();
foreach(get_all_category_ids() as $child_cat)
{
if(get_category($child_cat)->parent==$catalog_id)
{
$catalog_child_cats[]=$child_cat;
}
}
// Get the detailed category object
$category = get_category($cat);
$cat_parent_id = $category->cat_ID;
// Check if it is sub-category and having a parent, also check if the template file exists
if( (in_array($cat_parent_id, $catalog_child_cats)) && (file_exists(TEMPLATEPATH . '/catalog.php')) ) {
// Include the template for sub-sub-catgeory
include(TEMPLATEPATH . '/catalog.php');
exit;
}
return;
}
return;
}
add_action('template_redirect', 'catalog_template');
但它也为子类别使用catalog\u模板。如何为普通类别Catalog
及其子类别设置404.php
UPD2我在代码中发现了一个错误,需要更正
// Get the detailed category object
$category = get_category($cat);
$cat_parent_id = $category->category_parent;
我建议不要使用404,因为这意味着一些不同的、具体的东西;内容不存在或不可用,内容不是此分类法的子项
但是,如果你想。。。。。只需从404.php中复制代码,并将其嵌套在查询category+1级别的条件语句中。大概是这样的:
如果(是_类别(“目录”)){
$this_category=get_queryed_object();
如果(0!=$this_category->parent){
//来自404.php的代码
}}
我建议不要使用404,因为这意味着一些不同的、具体的东西;内容不存在或不可用,内容不是此分类法的子项
但是,如果你想。。。。。只需从404.php中复制代码,并将其嵌套在查询category+1级别的条件语句中。大概是这样的:
如果(是_类别(“目录”)){
$this_category=get_queryed_object();
如果(0!=$this_category->parent){
//来自404.php的代码
}}
// Get the detailed category object
$category = get_category($cat);
$cat_parent_id = $category->category_parent;