【问题标题】:Displaying Parent and Child Categories separately分别显示父类别和子类别
【发布时间】:2017-12-13 17:30:01
【问题描述】:

我想在网格中显示我的帖子,父类别与子类别分开显示,如下所示:

PARENT CATEGORY
Post Title
SUB/CHILD CATEGORY

目前我正在使用此代码:

<?php while ( have_posts() ) : the_post(); ?>
                    <a href="<?php the_permalink();?>" class="col col-sm-6 col-md-4 clearfix single-resource mix all <?php $category = get_the_category(); $secondCategory = $category[1]->cat_name; echo str_replace(' ', '', $secondCategory); ?> " data-ref="mixitup-target">
                                    <article class="resource-container">
                                        <div class="image"><?php the_post_thumbnail( 'medium' );  ?></div>
                                        <div class="details">
                                            <span class="cat">
                                            <?php $category = get_the_category();
                    $firstCategory = $category[0]->cat_name; echo $firstCategory;?>  </span>
                                            <h2><?php the_title();?></h2>
                                            <span class="cat-sub"><?php $category = get_the_category(); $secondCategory = $category[1]->cat_name; echo $secondCategory;?></span>

                                        </div>
                                    </article>
                                </a>
                <?php endwhile; ?>

但这似乎不一致,因为有时父母出现在孩子应该出现的地方,反之亦然。

我怎样才能让帖子显示在正确的位置,或者希望单独调用父类别和子类别?子类别也需要作为类添加到容器中。

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    您可以使用wp_get_object_terms 在循环开始时设置所有这些,然后使用变量不要每次都调用该函数..

    还要考虑超过 1 个父类别的情况。

    $parent_categories = wp_get_object_terms(get_the_ID(), 'category', array('parent'=>0));
    // can be more than 1 parent... so you maybe want to loop.
    $parent_category = $parent_categories[0]; // get first parent category
    // array of child categories for the parent category for this post.
    $child_categories = wp_get_object_terms(get_the_ID(), 'category', array('parent'=>$parent_category->term_id));
    $first_child_category = $child_categories[0]; // get first parent category
    

    这就是你应该如何使用它。您可以通过为类别添加循环来检查是否有超过 1 个父级或超过 1 个子级来改进它...

    您也可以在猫或子猫的容器之前的 html 中执行条件,以便在猫不存在时删除容器。

    对于类,最好使用类别 slug。

    <?php while ( have_posts() ) : the_post(); ?>
        <?php
        $parent_categories = wp_get_object_terms(get_the_ID(), 'category', array('parent'=>0));
        $cat_classes = array();
        // can be more than 1 parent... so you maybe want to loop.
        if($parent_categories) {
            $parent_category = $parent_categories[0]; // get first parent category
            $cat_classes[] = $parent_category->slug;
            // array of child categories for the parent category for this post.
            $child_categories = wp_get_object_terms(get_the_ID(), 'category', array('parent'=>$parent_category->term_id));
            if($child_categories) {
                $first_child_category = $child_categories[0]; // get first parent category
                $cat_classes[] = $first_child_category->slug;
            }
        }
    
        ?>
        <a href="<?php the_permalink();?>" class="col col-sm-6 col-md-4 clearfix single-resource mix all <?php echo implode(' ', $cat_classes); ?>" data-ref="mixitup-target">
            <article class="resource-container">
                <div class="image"><?php the_post_thumbnail( 'medium' );  ?></div>
                <div class="details">
                    <span class="cat">
                        <?php echo ($parent_category) ? $parent_category->name : ''; ?>
                    </span>
                    <h2><?php the_title();?></h2>
                    <span class="cat-sub">
                        <?php echo ($first_child_category) ? $first_child_category->name : ''; ?>
                    </span>
    
                </div>
            </article>
        </a>
    <?php endwhile; ?>
    

    【讨论】:

    • @creativepearson 我在答案中添加了代码,向您展示如何使用它。
    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 2012-10-14
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    相关资源
    最近更新 更多