WordPress企业主题开发中相关产品调用代码

释放双眼,带上耳机,听听看~!

WordPress企业主题开发中相关产品调用代码 (https://www.yunsxr.com/) WordPress开发教程 第1张

开发者在进行WordPress企业主题的时候,遇到有产品展示模块,需要在产品详情页底部做一个相关产品调用,今天就分享一个WordPress企业主题开发中相关产品调用代码。

<?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();

?>

人已赞赏
WordPress教程

WordPress自带数据库修复和优化工具

2020-6-24 9:52:36

WordPress教程

WordPress主题如何引用模板?

2020-6-24 10:18:35

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索