我想使用类别数组获取类别名称和子弹,以便以https://my_site.com/category/categoryslug的形式建立链接
锚链接将为<a href="https://my_site.com/category/categoryslug">categoryname</a>
function get_category_by_letter($letter){
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 48,
'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;
}
}
return $cats;
}
$the_let = $_GET['letter'];
$cats = get_category_by_letter($the_let);
var_dump($cats);
我希望输出具有链接的类别名称和子弹
答案 0 :(得分:0)
要获取类别链接,请使用带有{category-> term_id作为参数的get_category_link()
以获取ID。将以下代码添加到您的else语句中,如下所示:
if(strcasecmp($first_letter,$letter) != 0) continue;
else{
$cats[] = $category->term_id; //store category IDs in array
$cats[] = $category->name;
$cats[] = get_category_link($category->term_id);
}
}