欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」wordpress企业主题开发之相关产品调用代码

作者 : 自学哈 本文共1076个字,预计阅读时间需要3分钟 2022-11-26 共93人阅读
也想出现在这里? 联系我们

我们用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企业主题开发之相关产品调用代码
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号