有时在我们做WordPress主题开发的时候,可能会某一篇文章(包含自定义文章类型)同时发布在了多个分类中,或者说添加了多个标签,那么我们应该如何在改文章中获取所有带连接的标签或者带连接的某个分类法的所以分类呢?对此,WordPress中文社区自己写了一个自定义函数,您只需要把下面的代码放到您的functions.php文件中。
//获取文章所属所有带连接的分类(标签)
//www.wpshequ.cn
function get_myposts_tags($post_id,$taxonomy){
//获取指定文章ID的WP_Term对象数组
$terms = wp_get_post_terms($post_id,$taxonomy);
$tax_array = array();
//循环浏览当前产品的每个产品标签
if(count($terms)> 0){
foreach($terms as $term){
$term_id = $term-> term_id; //分类或标签ID
$term_name = $term-> name; //分类或标签名称
$term_slug = $term-> slug; //分类或标签slug
$term_link = get_term_link($term,$taxonomy); //分类或标签链接
//添加到数组
$tax_array[] ='<a href ="'.$term_link.'">'.$term_name.'</a>';
}
//以逗号分隔的分类或标签字符串设置数组,例如
$tax_array =implode(',',$tax_array);
//显示以逗号分隔的分类或标签字符串
return $tax_array;
}
}
然后在你需要显示的地方使用<?php echo get_myposts_tags('文章ID','分类法名称');?>Related: what is katie stam doing now, austin new church lgbt, vanderbilt football players in jail, what happened to trey on designated survivor, ammonia and water net ionic equation, lisa harbison lambert, accidents in boise today, stephanie duarte orange county, st tammany parish school board pay scale, methods of evaluating the effectiveness of the massage treatment, frankie boyle glasgow airport attack, chosin reservoir survivors list, henry ian cusick related to john cusack, physical signs chemo is working, council houses to rent in fleetwood,
还没有任何评论,你来说两句吧