我想通过缩略图和标题显示每个产品下面的4-5篇文章。对于所有产品,我添加了一个自定义分类法,将所有这些产品按其自己的组术语进行分组。可以说是一个自定义标记。
现在,我想在每个产品下查询5篇文章,它们共享为自定义分类法组选择的相同术语。到目前为止,我已经创建了这个工具来按分类法和术语查询帖子:
function shortcode_imwz_custom_taxonomy_by_term() {
global $wp_query,$post;
// wp_get_object_terms( $post->ID, \'portfolio-skills\', array( \'fields\' => \'names\' ) );
// https://stackoverflow.com/a/14798097/460885
// $product_terms = wp_get_object_terms( $post->ID, \'product\' );
// For performance, functions like get_the_terms() (which the results of has been cached),
// should be used.
// get the terms Retrieve the terms of the taxonomy that are attached to the post.
// $term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
// https://wordpress.stackexchange.com/a/21425/12260
$terms = get_the_terms( get_the_ID(), \'group\' );
$loop = new WP_Query( array(
\'posts_per_page\' => 5,
\'post_type\' => \'product\',
\'orderby\' => \'menu_order title\',
\'order\' => \'ASC\',
\'tax_query\' => array( array(
\'taxonomy\' => \'group\',
\'field\' => \'slug\',
\'terms\' => $terms
) )
) );
if( ! $loop->have_posts() ) {
return false;
}
while( $loop->have_posts() ) {
$loop->the_post();
// echo thumbnail
echo the_title();
}
wp_reset_postdata();
}
我根据这段代码添加了一个短代码function register_grouped_products_shortcode() {
add_shortcode( \'grouped-products\', \'shortcode_imwz_custom_taxonomy_by_term\' );
}
add_action( \'init\', \'register_grouped_products_shortcode\' );
要使用WP查询的产品,并添加了一个选项。但我还没有看到任何加载的内容。我错过了什么?我发现有很多选项可以让术语过滤分类法上的帖子和输入的术语。。也许这是个问题?