admin管理员组文章数量:1432222
I was wondering how I can get information from the wp_list_categories object just using the first character of the category name.
For example, I want to get the listing of categories based on the letter 'C'. How would I go about doing it?
Thank you, Kevin Davis
I was wondering how I can get information from the wp_list_categories object just using the first character of the category name.
For example, I want to get the listing of categories based on the letter 'C'. How would I go about doing it?
Thank you, Kevin Davis
Share Improve this question asked Apr 16, 2019 at 19:45 Kevin DavisKevin Davis 1071 gold badge2 silver badges6 bronze badges 1- Hi Ken, this question has been asked several times. Do a search here for "first letter" and you'll likely find what you're looking to accomplish. – rudtek Commented Apr 19, 2019 at 15:05
1 Answer
Reset to default 0Found a solution.
I found a function that answered my question.
Here is the function:
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);
if(strcasecmp($first_letter,$letter) != 0) continue;
else {
$cats[] = $category->term_id;
$cats[] = $category->name;
}
}
return $cats;
}
本文标签: Retrieving Categories based on the first character of the name
版权声明:本文标题:Retrieving Categories based on the first character of the name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581901a2664670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论