WordPress网站主题的产品推荐
WordPress网站主题文章可以多样化的推荐,那再为主题添加主题内容中的相关产品推荐, 利于用户在主题文章就能点击进入产品购物,更好的为用户服务,那么以下就为大家介绍相关应用的设置:
今天推荐大家一个常用的推荐同分类目录下文章的代码方式,只要将代码复制到文章底部,并对应添加我们的css样式就可以了,可以自由设置推荐文章数量和列表形式。代码如下:
<?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();
?>
0条评论