0

I have used this code

function show_categories_fn(){
    return wp_list_categories("echo=0&title_li");
}
add_shortcode('show_categories', 'show_categories_fn');

I got the category list but I need alphabetic letters shown here below:

  • A Apple Arkansas

  • B Bangkok Borat Bunghole

  • C Cat Copper

2
  • 1
    Are you on wordpress.com ? (you tagged the question with it). I'm still not sure what you're after here: You need to order the category list in alphabetic order but also add the alphabet letters too in front and then show only titles that start with A?
    – birgire
    Commented Nov 29, 2015 at 13:54
  • You mean you need first character or first character with word list
    – Kvvaradha
    Commented Nov 29, 2015 at 13:54

1 Answer 1

1

wp_list_categories() uses the Walker_Category walker which applies the list_cats filter, so a little PHP and:

function alpha_prefix_wpse_210205($cat) {
  $fl = substr($cat,0,1);
  $fl = strtoupper($fl);
  return $fl.' '.$cat;
}
add_filter('list_cats','alpha_prefix_wpse_210205');

You will want to apply the filter just before you need it and remove it afterwards.

function show_categories_fn(){
    add_filter('list_cats','alpha_prefix_wpse_210205');
    $lc = wp_list_categories("echo=0&title_li");
    remove_filter('list_cats','alpha_prefix_wpse_210205');
    return $lc;
}
add_shortcode('show_categories', 'show_categories_fn');

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