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;
}