如何在其他类别中显示子类别其他显示帖子

时间:2015-06-06 作者:Dima Skvarskyi

我有这样的层次结构:Category1-Subcategory1-Subcategory2-Post1Category2-Subcategory1

当我进入分类时,我需要显示子分类名称和描述///现在我使用以下代码:

    <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo \'<div id="catlist"><ul>\';
            $childcategories = get_categories(array(
                \'orderyby\' => \'name\',
                \'hide_empty\' => false,
                \'child_of\' => $this_category->cat_ID
            ));

        foreach($childcategories as $category) {
            echo \'<a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a>\';
            echo \'<p>\'.$category->description.\'</p>\';
        }
        echo \'</ul></div>\';
    }
}
?>
现在,我有子类别列表时,点击类别和空白页,如果我点击子类别链接,但我需要显示帖子时,我点击子类别列表。

4 个回复
SO网友:Andy Macaulay-Brook

您的代码检查子类别并显示它们是否存在,但您不会继续处理通常会显示当前类别中帖子的循环。

您希望将代码扩展为以下形式:

if ( is_category() ) {

    $this_category = get_category($cat);

    if ( get_category_children( $this_category->cat_ID ) != "" ) {

        // display the list of child categories as you currently do

    } else {

        /* run the standard loop to show the posts using
           whatever loop code your other templates use
        */

    }
}

SO网友:tayyab islam
<div id="content" role="main">
       <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo \'<div id="catlist"><ul>\';
            $childcategories = get_categories(array(
                \'orderyby\' => \'name\',
                \'hide_empty\' => false,
                \'child_of\' => $this_category->cat_ID
            ));
        foreach($childcategories as $category) {
            echo \'<a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a>\';
            echo \'<p>\'.$category->description.\'</p>\';
        } echo \'</ul></div>\';
    }
    else{       
        get_template_part(\'loop-header\');   
        if (have_posts()) :
            get_template_part(\'loop-actions\');
            get_template_part(\'loop-content\');
            get_template_part(\'loop-nav\');
        else :
            get_template_part(\'loop-error\');
        endif;  }}?>
<?php 
    ?>
    </div>
SO网友:Tanvir Ahmed Shaon
if (is_category()){
$category = get_queried_object();
$subcategories = get_category_children( $category->term_id );
// var_dump($subcategories);
if ( $subcategories != "" ) {
    echo \'<div id="catlist"><ul>\';
    $childcategories = get_categories(array(
        \'orderyby\' => \'name\',
        \'hide_empty\' => false,
        \'child_of\' => $category->term_id,
    ));
    foreach( $childcategories as $single ) {
       echo \'<a href="\' . get_category_link( $single->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $single->name ) . \'" \' . \'>\' . $single->name.\'</a>\';
       echo \'<p>\'.$single->description.\'</p>\';
    }
    echo \'</ul></div>\';
} else {
// your loop.
}
SO网友:Praveen

    <?php if ( have_posts() ) : ?><!-- .page-header -->
            <div class="clearfix enclose">
            <?php
            // Start the Loop.
            while ( have_posts() ) : the_post(); ?>
                <div class="sub-category-block">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'medium_thumbnail\'); ?>
                    <p class="title"><?php the_title(); ?></p>
                    </a>
                </div>
            <?php endwhile; ?>                        
            </div>
    <?php else : ?>
        <div class="page-header">
            <?php $args = array(
                \'posts_per_page\'   => 5,
                \'offset\'           => 0,
                \'category\'         => \'\',
                \'category_name\'    => \'\',
                \'orderby\'          => \'date\',
                \'order\'            => \'DESC\',
                \'include\'          => \'\',
                \'exclude\'          => \'\',
                \'meta_key\'         => \'\',
                \'meta_value\'       => \'\',
                \'post_type\'        => \'post\',
                \'post_mime_type\'   => \'\',
                \'post_parent\'      => \'\',
                \'author\'       => \'\',
                \'post_status\'      => \'publish\',
                \'suppress_filters\' => true 
                );?>
            <?php query_posts( $args ); ?>
            <?php while ( have_posts() ) : the_post(); ?>
            <?php endwhile; wp_reset_query(); ?>
        </div><!-- .page-header -->
    <?php endif; ?>
创建类别模板文件,例如类别abc。php并插入上述代码,您将获得重复父类别的所有子类别,当您单击子类别时,将显示子类别的帖子

结束

相关推荐

Parent and child categories

下面是a的一部分WP_Query 环我有两个父类别ABC和12,每个都有几个子类别。在第一个td 我想让ABC儿童频道的slug类和12个儿童频道的slug类。该职位可能属于两个子类别之一,或同时属于两个子类别。一个来自ABC,一个来自12,或者一个来自ABC,一个来自12。$inCat = get_the_category(get_the_ID()); echo \"<td>\".(!empty($inCat[0]->slug) ? $inCat[0]->slu