代码实现WordPress主题添加相关产品推荐
我们在 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(); ?>
提示:
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!邮箱:(332547532@qq.com)
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务 请大家谅解!
5. 如有链接无法下载、失效或广告,请点击右下方联系站长,可领回失去的金币,并额外有奖!
6. 如遇到加密压缩包,默认解压密码请在"下载框架提示方寻找",如遇到无法解压的请联系管理员!
黑域吧资源网 » 代码实现WordPress主题添加相关产品推荐
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!邮箱:(332547532@qq.com)
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务 请大家谅解!
5. 如有链接无法下载、失效或广告,请点击右下方联系站长,可领回失去的金币,并额外有奖!
6. 如遇到加密压缩包,默认解压密码请在"下载框架提示方寻找",如遇到无法解压的请联系管理员!
黑域吧资源网 » 代码实现WordPress主题添加相关产品推荐