[英]A-Z Index of Categories for Wordpress
有没有人看过任何wordpress插件,它们提供了类别的字母索引,可以带您进入属于特定字母的类别页面。 基本上,有一个列表:ABCD ... XYZ可以单击,当您单击A时,您会转到一个页面,在此页面中可以看到所有以字母A开头的类别?
我已经搜索了一个多小时,却找不到它...我开始搞砸了MySQL,将SELECT term_id as id, name as post_title FROM wp_terms ORDER BY name
但这还包括类别中的标记,我只想抓住类别。
更新:
好的,看来我可以通过以下方式从wp_term_taxonomy中获取类别:
SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = 'category'
然后,我可以获取该数组并在此处运行它:
SELECT term_id, name FROM wp_terms WHERE term_id IN (that-array) ORDER BY name
那个怎么样? 如何使用上面的内容创建新表?
您可以使用此功能并传递一个字母作为参数
function get_category_by_letter($letter){
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0);
$categories=get_categories($args);
foreach($categories as $category) {
$catname = $category->name;
$first_letter = substr(strip_tags($catname), 0 , 1); // get the first letter of the category
if(strcasecmp($first_letter,$letter) != 0) continue; //if not the same letter then loop next NOTE: this is case insensitive comparison
else{
$cats[] = $category->term_id; //store category IDs in array
//$cats[] = $category->name; uncomment this if you want to get category name
//or you can start your process here and remove the return value
}
}
return $cats;
}
用法示例
$cats = get_category_by_letter('A');
var_dump($cats);
您可以使用瞬时api来缓存您的sql查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.