0

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

  • 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 Apr 19 at 15:05
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;

   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.