有时候我们需要为网站添加一个自定义文章类型,但是当访问此文章类型页面时,如何获取相关联的文章呢?请看以下代码:
1 |
$terms = get_the_terms( $post->ID , 'product_tags', 'string'); $term_ids = wp_list_pluck($terms,'term_id'); $second_query = new WP_Query( array( 'post_type' => 'products', 'tax_query' => array( array( 'taxonomy' => 'product_tags', 'field' => 'id', 'terms' => $term_ids, 'operator'=> 'IN' //Or 'AND' or 'NOT IN' )), 'posts_per_page' => 3, 'ignore_sticky_posts' => 1, 'orderby' => 'rand', 'post__not_in'=>array($post->ID) ) ); if($second_query->have_posts()) { while ($second_query->have_posts() ) : $second_query->the_post(); //loop endwhile; wp_reset_query(); } |
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。