我正在尝试显示来自每个自定义分类的最新帖子。CPT用于“成员服务”,分类法是“服务”。
目前正在尝试合并我在网上找到的两个(Getting Custom Taxonomies和Getting Posts under Custom Taxonomy)脚本。
我不确定我错在哪里。
<!-- Add in list of member services -->
<?php // Get the taxonomy's terms
$terms = get_terms(
array(
'taxonomy' => 'services',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) { ?>
<a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a>
<?php
$post_args = array(
'numberposts' => 5,
'post_type' => 'services',
'services' => $term->term_id,
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
}
}
?>现实世界的逻辑:在主页上显示自定义分类下提供的最新成员服务(CPT) -“产品”、“价格”等。
Currently:显示自定义分类的列表,因此get_terms似乎可以正常工作。
发布于 2019-08-13 13:32:00
是的,您的get_terms正在工作,但您的get_posts不工作,因为您在post_type中传递了分类法名称。我对你的代码做了一些修改。也许这会对你有用
<?php
$post_args = array(
'posts_per_page' => 5,
'post_type' => 'member-services',
'orderby' => 'date',
'sort_order' => 'desc',
'tax_query' => array(
array(
'taxonomy' => 'services',
'field' => 'id',
'terms' => $term->term_id,
'include_children' => false
)
)
);
$posts = get_posts($post_args);
?>发布于 2019-08-13 13:45:41
您必须在get_posts()函数中编写tax_query。
只需替换
‘'services’术语$ => ->term_id,
使用
'tax_query‘术语数组( => (’分类法‘术语’服务‘,’字段‘术语'id',’术语‘术语$ => -> => _id))
https://stackoverflow.com/questions/57470285
复制相似问题