开发者在进行WordPress企业主题的时候,遇到有产品展示模块,需要在产品详情页底部做一个相关产品调用,今天就分享一个WordPress企业主题开发中相关产品调用代码。
<?php
//get the taxonomy terms of cus*** post type
$cus***TaxonomyTerms = 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’ => $cus***TaxonomyTerms
)
),
‘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=”https://www.wpzt.net/<?php the_per**link(); ?>”><?php the_post_thumbnail(‘product_thumb’); ?></a>
<a href=”https://www.wpzt.net/<?php the_per**link(); ?>”><?php the_title(); ?></a>
</li>
<?php
}
echo ‘</ul></div>’;
}else{
//no posts found
}
//restore original post data
wp_reset_postdata();
?>
暂无评论内容