WordPress.org

Support

Support » How-To and Troubleshooting » List Categories or Tags that starts with

List Categories or Tags that starts with

  • In one of my blogs Ive a lot of categories, categorisied in this way:

    A
    -Adam
    -Amaro
    -Aramo
    B
    -Bike
    -Books

    If I change this structure to
    Adam
    Amaro
    Aramo
    Bike
    Books

    Is there anyway to display categories that starts with A,
    or
    If I add Adam, Amaro… tags to posts is there anyway to list tags that starts with A.

    Thank you.

Viewing 1 replies (of 1 total)
  • This means going into your template file and adding the code that pulls out the categories you’d like to have appear.

    You’ll want to look at the PHP function strncmp and the WordPress function get_categories.

    Here’s one possible way to put them together:

    $categories=  get_categories();
    $letter = 'A';
    foreach ($categories as $category) {
        if ( strncmp($category->cat_name,$letter,1)==0) {
            $option = "<br/>";
            $option .= $category->cat_name;
            echo $option;
        }
    }

    You’ll undoubtedly want something more complicated than that, and there are some examples in the Codex page for get_categories that may be helpful.

    To do the same kind of thing with tags, you’d use the same get_categories function, but tell it you were interested in the “post_tag” taxonomy:

    $args=array(
      'taxonomy' => 'post_tag',
      );
    $tags = get_categories($args);

    I hope that’s enough to get you started.

Viewing 1 replies (of 1 total)
  • The topic ‘List Categories or Tags that starts with’ is closed to new replies.
Skip to toolbar