• 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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Marc Serra

    (@elseudomininet)

    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>, ';
            }

    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.

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

    Joebar, so what was it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Categories hierarchy in post page’ is closed to new replies.