【问题标题】:How to list subcategories of a category in categories page template (wordpress)如何在类别页面模板(wordpress)中列出类别的子类别
【发布时间】:2014-10-30 20:14:16
【问题描述】:

我有一个类别页面模板,列出了所有带有特色图片的类别。但我只想显示父类别的子类别。我不知道在哪里修改模板。这是代码。

get_header(); ?>

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

    <h1 class="border-radius-5"><?php the_title(); ?></h1>

        <div id="page" class="post-content">

            <?php the_content(); ?>

            <?php
                $terms = get_terms("category", $args);
                $count = count($terms);
                $categories = array();

                if ($count > 0) {

                echo '<ul class="listing-cat">';

                foreach ($terms as $term) {

                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field' => 'slug',
                                            'terms' => $term->slug
                                        )
                                    )
                );

                $video_from_categorie = new WP_Query( $args );

                if( $video_from_categorie->have_posts() ){

                    $video_from_categorie->the_post();

                }else{}

                $term->slug;
                $term->name;

            ?>

        <li class="border-radius-5 box-shadow">
            <?php echo get_post_image();?>

            <a href="<?php echo get_category_link( get_cat_ID($term->name) ); ?>" title="<?php echo $term->name; ?>"><span><?php echo $term->name; ?></span></a> 

            <span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
                _e( 'videos', get_theme_name() );
            }else{
                _e( 'video', get_theme_name() );
            } ?></span>                   
        </li>

    <?php
        }
        echo '</ul>';
        echo '<div class="clear"></div>';
        }
    ?>

    </div><!-- #page --> 

<?php endwhile; ?>

<?php endif; ?>

【问题讨论】:

    标签: php wordpress list categories


    【解决方案1】:

    将所需父术语/类别的 ID 传递给 get_terms()child_of 参数:

    $terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );
    

    【讨论】:

    • 然后关于这个代码函数$args);和 $args = array( 'post_type' => 'post', 'posts_per_page' => 1, 'show_count' => 1, 'orderby' => 'rand', 'post_status' => 'publish', 'tax_query' => 数组(数组('分类' => '类别','字段' => 'slug','terms' => $term->slug
    • 你能澄清你的问题吗?
    • 我的意思是从上面给出的代码中看到你用数组替换了 $args('child_of' => TERM_ID_GOES_HERE)); .那么 $args 函数作为 $args = array( 'post_type' => 'post', 'posts_per_page' => 1, 'show_count' => 1, 'orderby' => 'rand', 'post_status' => '发布', 'tax_query' => 数组(.
    • 将 $args 函数替换为 后是否也能正常工作?更换后会不会有效果或保持不变?
    • 那些 $args 仅适用于 foreach 循环内的查询。奇怪的是,您最初的 $args(用于 get_terms 的)从未定义过。
    猜你喜欢
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多