WordPress.org

Ready to get started?Download WordPress

Forums

List Categories or Tags that starts with (2 posts)

  1. atdheu
    Member
    Posted 1 year ago #

    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.

  2. converting2wp
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic