WordPress.org

Forums

Categories hierarchy in post page (5 posts)

  1. Marc Serra
    Member
    Posted 4 years ago #

    Good morning here! ;)

    I want to show the categories hierarchy in single post page, like in a sidebar but showing only the categories for this post.

    For example, I've this categories hierarchy:

    - Cat1
      - SubCat1.1
      - SubCat1.2
    - Cat2
      - SubCat2.1
      - SubCat2.2

    And I've a post with all categories asigned.

    Now, in the single post page I show the categories in this way...

    <?php
    foreach((get_the_category()) as $category) {
            echo '<a href="?cat=';
            echo $category->cat_ID . ' ';
            echo '">';
            echo $category->cat_name . ' ';
            echo '</a>, ';
            }
    ?>

    but this prints...

    Cat1, Cat2, SubCat1.1, SubCat1.2, SubCat2.1, SubCat2.2

    And I want to see ...

    Cat1, SubCat1.1, SubCat1.2, Cat2, Subcat2.1, SubCat2.2

    Do you understand me? ;)

    It's possible do that? How can I start?

    Thank's for your time.

  2. Marc Serra
    Member
    Posted 4 years ago #

    Ok, I find a solution, but requires to maintain the category SLUG uptaded!

    The following orders the post categories by slug text...

    $args=array('orderby' => 'slug');
    $terms = wp_get_post_terms( $post->ID , 'category', $args);
    foreach($terms as $term) {
      echo '<a href="' . esc_attr(get_term_link($term, 'category')) . '" title="' . sprintf(
    __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
    }

    Other options with different results (for who it's interested)...

    Prints a list of post categories in alphabetic order...
    echo get_the_category_list();

    Prints the post categories in alphabetic order...

    foreach((get_the_category()) as $category) {
           	echo '<a href="?cat=';
            echo $category->cat_ID . ' ';
            echo '">';
            echo $category->cat_name . ' ';
            echo '</a>, ';
            }

    Prints the post categories in alphabetic order and repeating the parent categories...

    foreach((the_category(', ',multiple,$post->ID)) as $category) {
            echo '<a href="?cat=';
            echo $category->cat_ID . ' ';
            echo '">';
            echo $category->cat_name . ' ';
            echo '</a>, ';
            }

    The following prints all the categories, not post categories...

    $args = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'orderby'                  => 'slug',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 0,
        'pad_counts'               => false
    );
    foreach((get_categories( $args )) as $category) {
            echo '<a href="?cat=';
            echo $category->cat_ID . ' ';
            echo '">';
            echo $category->cat_name . ' ';
            echo '</a>, ';
            }
  3. Joebar
    Member
    Posted 4 years ago #

    I have a slightly similar issue... In my archived results pages, I want to show the hierarchical list of categories a post belongs to..

    I'm currently using get_the_category_list('>') but that returns all categories in alphabetical order. I'd like to display them hierarchically. Is there a way to do this easily?

    The below is all I could find for syntax for this function. What are the options for $parents?

    get_the_category_list ([string $separator = ''], [string $parents = ''], [int $post_id = false])

    * string $separator: Optional, default is empty string. Separator for between the categories.
    * string $parents: Optional. How to display the parents.
    * int $post_id: Optional. Post ID to retrieve categories.

  4. Joebar
    Member
    Posted 4 years ago #

    ^^^^^^^ nevermind.... Had to do with how I was assigning categories to posts.

  5. lerizzle
    Member
    Posted 3 years ago #

    Joebar, so what was it?

Topic Closed

This topic has been closed to new replies.

About this Topic