分别显示 Parent 和 Child 类别
Displaying Parent and Child Categories separately
我想在网格中显示我的帖子,Parent 类别与子类别分开显示,如下所示:
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; ?>
但这似乎不一致,因为有时 Parent 出现在 child 应该出现的位置,反之亦然。
如何让帖子显示在正确的位置,或者希望只分别调用 parent 和子类别? child 类别也需要作为 class 添加到容器中。
你可以使用 wp_get_object_terms
在循环开始时设置所有这些然后使用变量不要每次都调用函数..
还要考虑超过 1 个 parent 类别的情况。
$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 parent 或超过 1 child...
您还可以在猫或 child 猫的容器之前执行 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; ?>
我想在网格中显示我的帖子,Parent 类别与子类别分开显示,如下所示:
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; ?>
但这似乎不一致,因为有时 Parent 出现在 child 应该出现的位置,反之亦然。
如何让帖子显示在正确的位置,或者希望只分别调用 parent 和子类别? child 类别也需要作为 class 添加到容器中。
你可以使用 wp_get_object_terms
在循环开始时设置所有这些然后使用变量不要每次都调用函数..
还要考虑超过 1 个 parent 类别的情况。
$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 parent 或超过 1 child...
您还可以在猫或 child 猫的容器之前执行 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; ?>