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();
?>
0条评论