user1937021
user1937021

Reputation: 10771

Categories not appearing in correct hierarchy

When I create a post and try to assign the post to categories, the widget on the right-hand side shows the categories mixed up. For example, when I created the categories, I had the "accessories" category under the parent category "Men", but in the widget it doesn't appear under it; only when I go to category on the left-hand side menu does it appear in the correct order. What is wrong?

In categories I have this hierarchy

enter image description here

but when I assign a post to but when I assign a post to categories for example Men-Accesories-Ties, Men appears second in categories and not first thus messing up the breadcrumbs too

enter image description here

Upvotes: 0

Views: 2779

Answers (1)

brasofilo
brasofilo

Reputation: 26055

I'll leave the first version of this Answer at the bottom, as it may be useful.

There is no way of organizing that column in a hierarchical way. The solution is to make a custom column and use this Codex snippet:

// Change "post_" for the desired post type
add_filter( 'manage_edit-post_columns', 'custom_categories_register_so_15813936', 20, 1 );
add_action( 'manage_post_posts_custom_column', 'custom_categories_display_so_15813936', 20, 2 );

function custom_categories_register_so_15813936( $columns ) 
{
    $columns[ 'custom-cat' ] = 'Categories';
    return $columns;
}

function custom_categories_display_so_15813936( $column_name, $post_id ) 
{
    if ( 'custom-cat' != $column_name )
        return;

    // get the category IDs assigned to post
    $categories = wp_get_post_categories( $post_id, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';

    if ( $categories ) {
        $cat_ids = implode( ',' , $categories );
        $cats = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $cat_ids );
        $cats = rtrim( trim( str_replace( '<br />',  $separator, $cats ) ), $separator );
        $cats = str_replace( site_url('category/'), admin_url('edit.php?category_name='), $cats );
        echo str_replace( '/" title', '" title', $cats );
    }
}

At the left, the default column, and at the right, the custom one.

enter image description here


[first version]
I suppose you are talking about the back-end (/wp-admin). And the behavior you see is WordPress default. To get rid of that "feature", you need the plugin Category Checklist Tree:

On the post editing screen, after saving a post, you will notice that the checked categories are displayed on top, breaking the category hierarchy. This plugin removes that "feature".

Additionally, it automatically scrolls to the first checked category.

Works with custom taxonomies too.

Upvotes: 1

Related Questions