WordPress企业主题开发中相关产品调用代码

开发者在进行WordPress企业主题的时候,遇到有产品展示模块,需要在产品详情页底部做一个相关产品调用,今天…

WordPress企业主题开发中相关产品调用代码 (https://www.yunsxr.com/) WordPress开发教程 第1张

开发者在进行WordPress企业主题的时候,遇到有产品展示模块,需要在产品详情页底部做一个相关产品调用,今天就分享一个WordPress企业主题开发中相关产品调用代码。

<?php

//get the taxonomy terms of custom post type

$customTaxonomyTerms = wp_get_object_terms( $post->ID, ‘product_category’, array(‘fields’ => ‘ids’) );

//query arguments

$args = array(

‘post_type’ => ‘products’,

‘post_status’ => ‘publish’,

‘posts_per_page’ => 4,

‘orderby’ => ‘rand’,

‘tax_query’ => array(

array(

‘taxonomy’ => ‘product_category’,

‘field’ => ‘id’,

‘terms’ => $customTaxonomyTerms

)

),

‘post__not_in’ => array ($post->ID),

);

//the query

$relatedPosts = new WP_Query( $args );

//loop through query

if($relatedPosts->have_posts()){

echo ‘<div class=”related-products”>’;

echo ‘<h3>’ . __(‘Related Products’) . ‘</h3>’;

echo ‘<ul>’;

while($relatedPosts->have_posts()){

$relatedPosts->the_post();

?>

<li>

<a href=”<?php the_permalink(); ?>”><?php the_post_thumbnail(‘product_thumb’); ?></a>

<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>

</li>

<?php

}

echo ‘</ul></div>’;

}else{

//no posts found

}

//restore original post data

wp_reset_postdata();

?>

类别:WordPress技巧

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT