admin管理员组文章数量:1125961
I am using Query Loop Block
to list all posts under a custom post type. But I want to display all posts under each individual category, is that possible using query block?
Something like this:
Category 1:
- Post 1
- Post 2
- Post 3
Category 2:
- Post 4
- Post 5
- Post 6
Category 3:
- Post 7
- Post 8
- Post 9
where categories are custom taxonomies for the post type, I dont want to manually add the category or create 3 custom query loop and filter for each taxonomy. I want to just use 1 block and list all posts under each category.
I am using Query Loop Block
to list all posts under a custom post type. But I want to display all posts under each individual category, is that possible using query block?
Something like this:
Category 1:
- Post 1
- Post 2
- Post 3
Category 2:
- Post 4
- Post 5
- Post 6
Category 3:
- Post 7
- Post 8
- Post 9
where categories are custom taxonomies for the post type, I dont want to manually add the category or create 3 custom query loop and filter for each taxonomy. I want to just use 1 block and list all posts under each category.
Share Improve this question asked Feb 28, 2024 at 3:30 Rain ManRain Man 1642 silver badges15 bronze badges 3- 3 It’s not possible with the query loop block. You would need to manually add a query loop block for each category. – Jacob Peattie Commented Feb 28, 2024 at 5:00
- 1 @JacobPeattie yeah, I was wondering if there some sort of hook to modify the output but i think better way is just to create my own block. – Rain Man Commented Feb 28, 2024 at 13:01
- Not really. What you're trying to do is sufficiently different from the way the block normally works that it's probably not feasible. A custom block would be the way to go. – Jacob Peattie Commented Feb 28, 2024 at 22:54
1 Answer
Reset to default -2Use WP_Query
directly in your WP theme template files instead of a block editor.
// Define custom taxonomy (category) terms
$categories = get_terms(array(
'taxonomy' => 'your_custom_taxonomy', // Change 'your_custom_taxonomy' to the name of your taxonomy
'hide_empty' => false,
));
// Loop through each category
foreach ($categories as $category) {
echo '<h2>' . $category->name . '</h2>'; // Display category name
echo '<ul>';
// Query posts based on the current category
$category_query = new WP_Query(array(
'post_type' => 'your_custom_post_type', // Change 'your_custom_post_type' to the name of your custom post type
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy', // Change 'your_custom_taxonomy' to the name of your taxonomy
'field' => 'term_id',
'terms' => $category->term_id,
),
),
));
// Loop through posts in the current category
while ($category_query->have_posts()) {
$category_query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; // Display post title with permalink
}
echo '</ul>';
// Reset post data
wp_reset_postdata();
}
本文标签: Using Query Loop Block to list all posts under each category
版权声明:本文标题:Using Query Loop Block to list all posts under each category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736636702a1945899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论