douchen1988
2016-08-13 14:56
浏览 68
已采纳

如何使用wordpress中的子项为特定类别创建类别和单页的自定义页面

I want to create custom page for category and single page for specifed category with its children .

First , to create custom single page i've used this code

if (in_category('blog')){
include (TEMPLATEPATH . '/single-blog.php');
}elseif(in_category('projects')){
    include (TEMPLATEPATH . '/single-projects.php');
}
else{
    include (TEMPLATEPATH . '/single-default.php');
}

and it work fine just for specifed ctegory in the code and doesn't support the children of category.

fo example : i would like to use single-blog.php for single page of posts that its category is blog or children of blog .

Second , for category page i want to do the same things that i've explained above for list of category's posts .

fo example : i would like to show the list of posts that is related to blog category or its children in category-blog.php.

how can i do it.

图片转代码服务由CSDN问答提供 功能建议

我想为类别和单页创建自定义页面以及其子项。

首先,要创建自定义单页我已使用此代码

  if(in_category('blog')){\  ninclude(TEMPLATEPATH。'/ single-blog.php'); 
n} elseif(in_category('projects))/
 include(TEMPLATEPATH。'/ single-project.php'); 
} 
else {
}  n include(TEMPLATEPATH。'/ single-default.php'); 
n} 
   
nn 

它只适用于代码中指定的ctegory并且不能正常工作 支持类别的孩子。

示例:我想将 single-blog.php 用于其类别 blog的单页帖子 博客的儿童

第二,对于类别页面,我想做与上面针对类别帖子列表所解释的相同的事情。

示例:我想在 category-blog.php 中显示与博客类别或其子项相关的帖子列表。

如何 我能做到吗。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglu2523 2016-08-13 16:33
    已采纳

    For the first part of your question, you're probably looking for cat_is_ancestor_of, so you'd write something like this:

    function is_ancestor_of( $ancestor_category ) {
        $categories = get_the_category(); // array of current post categories
        foreach ( $categories as $category ) {
            if ( cat_is_ancestor_of( $ancestor_category, $category ) ) return true;
        }
        return false;
    }
    
    $ancestor_category = get_category_by_slug( 'blog' );
    if ( in_category( 'blog' ) || is_ancestor_of( $ancestor_category ) ) {
        // include
    }
    

    For the second part, I understand that you want to do the same thing but for an archive page. In that case, it's a bit simpler as you won't have an array of categories:

    $archive_category = get_category( get_query_var( 'cat' ) ); // current archive category
    $ancestor_category = get_category_by_slug( 'blog' );
    if ( is_category( 'blog' ) || cat_is_ancestor_of( $ancestor_category, $archive_category ) {
        // include
    }
    

    Let me know if this works for you,

    EDIT - Here's an alternative option (not tested) that doesn't use --at least directly-- a foreach loop. Not sure if it's higher performance though.

    $ancestor_category = get_category_by_slug( 'blog' );
    $children_categories = get_categories( array ( 'parent' => $ancestor_category->cat_ID ) ); // use 'child_of' instead of 'parent' to get all descendants, not only children
    
    $categories = get_the_category(); // array of current post categories
    
    if ( in_category( 'blog' ) || ! empty( array_intersect( $categories, $children_categories ) ) {
        // include
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题