WordPress获取自定义文章类型相关文章

我们在主题开发过程中,如果用到了自定义文章类型,有时候我们还需要去获取自定义文章类型的相关文章,比如你创建了一个product自定义文章类型,和其对应的自定义分类法是product_category。那么,如果我们需要在产品详情页获取相关产品的话,我们就可以使用一下代码

<?PHP

$terms = get_the_terms( $post->ID,'product_category','string');/**product_category是你的自定义分类法的名字**/

$term_ids = wp_list_pluck($terms,'term_id');

$second_query = new WP_Query( array(

'post_type' => 'product',/**product是你自定义文章类型的名字**/

'tax_query' => array(

array(

'taxonomy' => 'product_category',/**product_category是你自定义分类法的名字**/

'field' => 'id',

'terms' => $term_ids,

'operator'=> 'IN' //Or 'AND' or 'NOT IN'

)),

'posts_per_page' => 4,

'ignore_sticky_posts' => 1,

'orderby' => 'rand',

'post__not_in'=>array($post->ID)

) );

?>

<?PHP if($second_query->have_posts())?>

<?PHP while ($second_query->have_posts() ) : $second_query->the_post();?>

/**你的循环获取内容**/

<?PHP endwhile; wp_reset_query();?>

 

相关文章

我们有时候在定制WORDPRESS主题的时候,由于菜单样式的要求我...
很多朋友在做wordpree主题制作的时候会经常遇到一个问题,那...
wordpress后台的模块很多,但并不是每个都经常用到。介绍几段...
从WordPress4.2版本开始,如果我们在MYSQL5.1版本数据中导出...
很多网友会遇到这样一个问题,就是WordPress网站上传图片、附...
对于经常要在文章中出现代码的IT相关博客,安装一个代码高亮...