我有一个名为 Cable 的产品类别,我想获取分配给该类别的所有帖子并输出所述帖子的标题。此类别来自 woo-commerce 添加的自定义分类“类别”,我不确定这是否会使事情变得更难?但我还没有找到解决方案。
有人可以帮忙吗?
最佳答案
试试这段代码:
并根据您的要求进行修改:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
关于php - 如何显示分配相同类别的所有帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74458966/