有时候我们需要为网站添加一个自定义文章类型,但是当访问此文章类型页面时,如何获取相关联的文章呢?请看以下代码:
$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(); }
看以上代码即可知道方法,哈哈哈~~小白请绕道或联系模板兔开发
0 个评论