加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

代码实现WordPress主题添加相关产品推荐

发布时间:2020-12-14 14:31:14 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 我们在 wordpress 企业主题开发中,常常会用到相关产品推荐的代码,方便访客根据我们的当前产品页跳转到其它的产品对,增强访客的购买兴趣,那对于那些

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

我们在 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();

?>

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读