• Hello,

    I have a lyrics site, so I have a lot of categories.
    I have pages A, B, C…
    Example: How to list categories that starts with A*

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    //list categories if begin with letter a/A
    $categories=get_categories();
    if ($categories) {
      foreach($categories as $category) {
        if ( strtoupper(substr($category->name,0,1)) =='A' ) {
          echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></p> ';
        }
      }
    }
    ?>

    Michael you’ll get faster results with get_terms (it’s thousandths of a second, but it’s 5-9 times faster)..

    $categories = get_terms('category', 'name__like=a%');
    //$categories = get_terms('category', 'name__like=A%');
    if ($categories) {
    	foreach($categories as $category) {
    		echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></p> ';
    	}
    }

    Took your code and switched to get_terms…
    name__like paramter is perfect for this case to..

    Thanks Mark. I will try to remember that ??

    gcarson

    (@gcarson)

    What would be the code to see if it starts with a number? i tried name__like= ‘num’ or ‘number’ but it didn’t seem to work. Any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List categories starts with A*’ is closed to new replies.