WordPress教程

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

阿里云

我们在 WordPress 企业主题开发中,常常会用到相关产品推荐的代码,方便访客根据我们的当前产品页跳转到其它的产品对,增强访客的购买兴趣,那对于那些没有推荐产品功能的 wordpress 主题,怎样添加这样的推荐呢?

下面给大家推荐一个常用的推荐同分类目录下文章的代码方式,只要将下面的代码复制到我们的文章底部,并对应添加好我们的 css 样式就可以了,而且里面的参数都是可以设置的,可以自由设置推荐的文章数量和列表形式。

也想出现在这里?联系我们
创客主机
  1. <?php
  2.  //get the taxonomy terms of custom post type
  3.  $customTaxonomyTerms = wp_get_object_terms( $post->ID, 'product_category', array('fields' => 'ids') );
  4.  //query arguments
  5.  $args = array(
  6.      'post_type' => 'products',
  7.      'post_status' => 'publish',
  8.      'posts_per_page' => 4,
  9.      'orderby' => 'rand',
  10.      'tax_query' => array(
  11.          array(
  12.             'taxonomy' => 'product_category',
  13.              'field' => 'id',
  14.              'terms' => $customTaxonomyTerms
  15.          )
  16.      ),
  17.      'post__not_in' => array ($post->ID),
  18.  );
  19.  //the query
  20.  $relatedPosts = new WP_Query( $args );
  21.  //loop through query
  22.  if($relatedPosts->have_posts()){
  23.  echo '<div class="related-products">';
  24.  echo '<h3>' . __('Related Products') . '</h3>';
  25.      echo '<ul>';
  26.      while($relatedPosts->have_posts()){ 
  27.          $relatedPosts->the_post();
  28.  ?>
  29.      <li>
  30.       <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('product_thumb'); ?></a>
  31.       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  32.      </li>
  33.  <?php
  34.      }
  35.      echo '</ul></div>';
  36.  }else{
  37.      //no posts found
  38.  }
  39.  //restore original post data
  40.  wp_reset_postdata();
  41.  ?>

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

已有 406 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!