admin管理员组文章数量:1426308
I want to show only the sub-categories from a post's specific parent category and exclude all the other categories.
This is for a loop of recent posts on my front-page.php
. Each recent post is currently listing its categories using get_the_category_list($post->ID)
.
I tried get_the_category_list($parent, '8')
but it didn't exclude anything. 8 is the integer of the parent category I want to keep. Any help is much appreciated!
Here's my code:
<?php $the_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'genres',
'posts_per_page' => 6,
'post_status' => 'publish'
));
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="wrapper">
<div> // stuff with post title, image and excerpt </div>
<div>' . get_the_category_list( $post->ID ) . '</div>';
endwhile;
wp_reset_postdata(); ?>
I want to go from this:
Parent-category 1, Sub-category 1 , Parent-category 2, Sub-category 2, Parent-category 3, Sub-category 3
To this:
Sub-category 1
I want to show only the sub-categories from a post's specific parent category and exclude all the other categories.
This is for a loop of recent posts on my front-page.php
. Each recent post is currently listing its categories using get_the_category_list($post->ID)
.
I tried get_the_category_list($parent, '8')
but it didn't exclude anything. 8 is the integer of the parent category I want to keep. Any help is much appreciated!
Here's my code:
<?php $the_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'genres',
'posts_per_page' => 6,
'post_status' => 'publish'
));
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="wrapper">
<div> // stuff with post title, image and excerpt </div>
<div>' . get_the_category_list( $post->ID ) . '</div>';
endwhile;
wp_reset_postdata(); ?>
I want to go from this:
Parent-category 1, Sub-category 1 , Parent-category 2, Sub-category 2, Parent-category 3, Sub-category 3
To this:
Sub-category 1
Share Improve this question edited May 26, 2019 at 22:31 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked May 26, 2019 at 22:09 BlueHelmetBlueHelmet 1791 silver badge10 bronze badges1 Answer
Reset to default 0I was able to find a function that worked found here.
First, I added the below code to my functions.php
, which excludes the parent categories I do not want:
function custom_get_the_category_list() {
$categories = get_the_category();
$exclude1 = get_cat_ID( 'Parent1' );
$exclude2 = get_cat_ID( 'Parent2' );
$categories_list = '';
foreach ( $categories as $category ) {
if ( $category->category_parent == $exclude1
|| $category->cat_ID == $exclude1 || $category->category_parent == $exclude2
|| $category->cat_ID == $exclude2 ) {
continue;
}
$categories_list .=
'<li><a href="' . get_category_link( $category->cat_ID ) . '">' .
$category->name .
'</a></li>';
}
return '<ul class="post-categories">' . $categories_list . '</ul>';
}
And then in my loop, instead of using get_the_category_list($post->ID)
, I used the new function custom_get_the_category_list()
.
It certainly works, but if anyone finds a simpler solution please post.
本文标签: wp queryExclude parent categories from recent posts list
版权声明:本文标题:wp query - Exclude parent categories from recent posts list 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745462212a2659375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论