Products
GG网络技术分享 2025-03-18 16:12 0
wordpress中的term表主要是:wp_terms,wp_term_taxonomy 和 wp_term_relationships 表,若是用好这几个表,可以使wordpress从博客变为cms,甚至其它类型的网站,知道他的强大了吧,那么我们一起看看吧。
wp_terms
wp_term_taxonomy (无限分类表结构)
wp_term_relationships
从图可以看出来,wp_term_relationships 是一个关联表,它链接的是 wp_posts 和 wp_links ,因此object_id,对应的是文章ID或者链接ID。
wp_term_taxonomy相当于wp_terms的扩展表,这几个表可以让wordpress创建不同的分类,并且wordpress还提供很多方法,用于调用这几个表的数据。
我们在做wordpress网站的时候,常常会新增不同的文章类型来拓展网站的功能。
今天给大家分享如何调用自定义文章类型的相关文章。
只需要在自定义文章类型的文章页调用下面代码就可以实现功能:
<?php//获取自定义文章类型的分类项目
$custom_taxterms=wp_get_object_terms($post->ID,'product-category',array('fields'=>'ids'));
//参数
$args=array(
'post_type'=>'product',//文章类型
'post_status'=>'publish',
'posts_per_page'=>12,//文章数量
'orderby'=>'rand',//随机排序
'tax_query'=>array(
array(
'taxonomy'=>'product-category',//分类法
'field'=>'id',
'terms'=>$custom_taxterms
)
),
'post__not_in'=>array($post->ID),//排除当前文章
);
$related_items=newWP_Query($args);
//查询循环
if($related_items->have_posts()):
echo'';
while($related_items->have_posts()):$related_items->the_post();
$url=wp_get_attachment_url(get_post_thumbnail_id($post->ID));
?>
<liclass="amazingcarousel-item">
<divclass="amazingcarousel-item-container">
<divclass="amazingcarousel-image">
<ahref="<?phpthe_permalink();?>"title="MultiStation"class=""><imgsrc="<?phpecho$url;?>"alt="MultiStation"width="240"height="180"/></a></div>
<divclass="amazingcarousel-title"><arel="external"href="<?phpthe_permalink();?>"><?phpthe_title();?></a></div></div>
</li>
<?php
endwhile;
echo'</ul>';
endif;
//重置文章数据
wp_reset_postdata();
?>
只需要替换上面的文章类型和分类法即可,调用数目自己修改。
显示效果如下:
Demand feedback