简体   繁体   English

如何只显示在WordPress的父类别页面中的子类别?

[英]how to show only subcategories in parent category page in wordpress?

I'm wondering how to show just only subcategories on a parent category? 我想知道如何仅显示父类别中的子类别?

Is there any way to do this? 有什么办法吗?

I'm using this code. 我正在使用此代码。

<?php
if (is_category())
{
$cur_cat = get_query_var('cat');
if ($cur_cat) 
   {
    $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat  .'&depth=1&title_li=&&show_count=1&hide_empty=0');
    echo '<ul>' . $new_cats . '</ul>';
   }
}
?>

I have parent category videos and 3 child categories of that art,technology and funny. 我有父类别的视频和3种艺术,技术和有趣的子类别。

Now I just want to show only 3 child categories in category:videos 现在,我只想在以下category:videos显示3个子category:videos

But my problem is that there is also showing parent category posts. 但是我的问题是,也显示了父类别的帖子。 And when I go to any of child category page, there is show this line No categories on the category title. 当我转到任何子类别页面时,在类别标题上都显示此行No categories For example, when I go to art category page, then on the category title of Art this line is shown. 例如,当我去艺术类网页,然后在类别标题Art显示这一行。

Please suggest me complete and useful solution. 请建议我完整和有用的解决方案。

UPDATED 更新

Here is my entire category.php template: 这是我的整个category.php模板:

<?php get_header(); ?>

<article class="content">
<div id="marquee-container"><div  title="News Updates…" alt="News Updates…"  style="position:relative;  background-color: #FFF; width:137px; height:19px; z-index:1; border-radius:3px; background-image:url(<?php bloginfo('template_directory'); ?>/images/wp0465f11b_06.png); display:inline-block;"></div><h4 class="marquee-text">New Features Coming Soon...</h4></div>

    <?php if ( have_posts() ) : ?>
        <div class="posts">
            <h1 class="archive-title"><?php printf( __( 'Category: %s', 'OnlinEarn' ), single_cat_title( '', false ) ); ?></h1>

            <?php if ( category_description() ) : // Show an optional category description ?>
            <div class="archive-meta"><?php echo category_description(); ?></div>
            <?php endif; ?>
        <?php $args = array('child_of' => 'videos'); ?>
            <?php
            if (is_category('videos'))
            {
            $cur_cat = get_query_var('cat');
            if ($cur_cat) 
               {
                $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat  .'&depth=1&title_li=&&show_count=1&hide_empty=0');

               }
            }
        ?>


        <?php /* The loop */ ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <div class="block"><?php get_template_part( 'content', get_post_format() ); ?></div>

            <?php endwhile; ?>


        </div><!--.posts-->


    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>

    </article><!-- .content -->
<?php get_header(); ?>

<article class="content">
<div id="marquee-container"><div  title="News Updates…" alt="News Updates…"  style="position:relative;  background-color: #FFF; width:137px; height:19px; z-index:1; border-radius:3px; background-image:url(<?php bloginfo('template_directory'); ?>/images/wp0465f11b_06.png); display:inline-block;"></div><h4 class="marquee-text">New Features Coming Soon...</h4></div>

    <?php if ( have_posts() ) : ?>
        <div class="posts">
            <h1 class="archive-title"><?php printf( __( 'Category: %s', 'OnlinEarn' ), single_cat_title( '', false ) ); ?></h1>

            <?php if ( category_description() ) : // Show an optional category description ?>
            <div class="archive-meta"><?php echo category_description(); ?></div>
            <?php endif; endif;

            $category = get_the_category();

            if ($category[0]->category_parent != '' && is_category('video')) :

             //show child categories
            $cur_cat = get_query_var('cat');

                $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat  .'&depth=1&title_li=&&show_count=1&hide_empty=0');
                echo '<ul>' . $new_cats . '</ul>';

            elseif ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                <div class="block"><?php get_template_part( 'content', get_post_format() ); ?></div>

            <?php endwhile; ?>


        </div><!--.posts-->


    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>

    </article><!-- .content -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM