【问题标题】:how to show only subcategories in parent category page in wordpress?如何在wordpress的父类别页面中仅显示子类别?
【发布时间】:2014-01-28 13:42:32
【问题描述】:

我想知道如何仅显示父类别的子类别?

有什么办法吗?

我正在使用此代码。

<?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>';
   }
}
?>

我有父类别视频和 3 个艺术、技术和有趣的子类别。

现在我只想在category:videos 中显示 3 个子类别

但我的问题是还有显示父类别的帖子。当我转到任何子类别页面时,类别标题上会显示此行No categories。 例如,当我进入艺术分类页面时,在Art的分类标题上会显示这一行。

请建议我完整且有用的解决方案。

更新

这是我的整个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 wordpress


【解决方案1】:
<?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 -->

【讨论】:

  • 请给出else { //loop here to list posts on child category pages }之后的帖子子类别页面列表的循环示例
  • 其实我没有使用archive.php文件,我使用的是category.php模板
  • 好吧,我尝试了您的代码,但仍然无法正常工作。 :(
  • 检查我更新的答案。我测试过,这应该对您有用,但仅适用于您的视频父类别。如果您希望它适用于所有父类别,请删除 &amp;&amp; is_category('video') 表达式。希望它有效。
猜你喜欢
  • 2014-04-09
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多