Retrieving Categories based on the first character of the name

I was wondering how I can get information from the wp_list_categories object just using the first character of the categ

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
Add a comment  | 

1 Answer 1

Reset to default 0

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

   }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745581902a4634304.html

相关推荐

  • Retrieving Categories based on the first character of the name

    I was wondering how I can get information from the wp_list_categories object just using the first character of the categ

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信