「自学哈网」wordpress企业主题开发之相关产品调用代码
也想出现在这里?
联系我们吧
我们用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();
?>
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!
自学哈网 » 「自学哈网」wordpress企业主题开发之相关产品调用代码
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!
自学哈网 » 「自学哈网」wordpress企业主题开发之相关产品调用代码
也想出现在这里?
联系我们吧