<?php // 获取自定义文章类型的分类项目 $custom_taxterms = wp_get_object_terms( $post->ID,'分类项目别名', array('fields' => 'ids') ); // 参数 $args = array( 'post_type' => '文章类型别名',// 文章类型 'post_status' => 'publish', 'posts_per_page' => 4, // 文章数量 'orderby' => 'rand', // 随机排序 'tax_query' => array( array( 'taxonomy' => '分类法别名', // 分类法 'field' => 'id', 'terms' => $custom_taxterms ) ), 'post__not_in' => array ($post->ID), // 排除当前文章 ); $related_items = new WP_Query( $args ); // 查询循环 if ($related_items->have_posts()) : while ( $related_items->have_posts() ) : $related_items->the_post(); ?> //这里是循环的内容// <?php endwhile; endif; ?> 有必要的话后面可以重置下数据。 <?php wp_reset_postdata(); ?>
有必要的话后面可以重置下数据。