1

I bought I real estate theme, I need a child category for Community custom taxonomy so i set the Hierarchical to True. But when I tried displaying it in my page the Community comes first before the sub community. This is what it displays:

"Business Bay, Business Tower, Dubai, UAE"

instead of

"Business Tower, Business Bay, Dubai, UAE"

Business towers is the child, Business bay is the parent. It should be child first before the parent right?

can you please help me.

here is the screenshot when i display the community

Here is the code:

    $communitylabels = array(
            'name' => __( 'Community', 'contempo' ),
            'singular_name' => __( 'Community', 'contempo' ),
            'search_items' =>  __( 'Search Communities', 'contempo' ),
            'popular_items' => __( 'Popular Communities', 'contempo' ),
            'all_items' => __( 'All Communities', 'contempo' ),
            'parent_item' => __( 'Parent', 'contempo' ),
            'parent_item_colon' => __( 'Parent', 'contempo' ),
            'edit_item' => __( 'Edit Community', 'contempo' ),
            'update_item' => __( 'Update Community', 'contempo' ),
            'add_new_item' => __( 'Add New Community', 'contempo' ),
            'new_item_name' => __( 'New Community', 'contempo' ),
            'separate_items_with_commas' => __( 'Separate Communities with commas', 'contempo' ),
            'add_or_remove_items' => __( 'Add or remove Communities', 'contempo' ),
            'choose_from_most_used' => __( 'Choose from the most used Communities', 'contempo' )
        );
        register_taxonomy( 'community', 'listings', array(
            'hierarchical' => true,
            'labels' => $communitylabels,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'community' ),
        ));
    }

    if ( ! function_exists( 'community' ) ) {
        function community() {
            global $post;
            global $wp_query;
            $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'community', '', ', ', '' ) );
            echo esc_html($terms_as_text);
        }
    }

Here is the code when i call the custom taxonomy:

      // Title
        case 'listing_title' :

           echo '<h2 class="marT5 marB0">';
                ct_listing_title();
            echo '</h2>';

        break;

        // location
        case 'listing_city_state_zip' :

            echo '<p class="location marB0">';  
            community();
            echo ', ';     
                city();
                echo '';
                country();

            echo '</p>';
2
  • Actually, the parent comes first, your query is right Commented Jul 26, 2017 at 14:30
  • Hello, I edited my post can you please check again? i add some more details. Thanks. Commented Jul 26, 2017 at 14:47

1 Answer 1

0

If you want to sort your terms by hierarchy, you can use wp_get_post_terms() instead.

It's usage is similar to get_the_term_list(). Take a look at this simple example:

wp_get_post_terms( $post->ID, $taxonomy->name, array( 'orderby' => 'term_id' ) );

I think we can safely assume that the parent will mostly have a lower ID of its childs (if not manually changed), so this will do the trick. But if the post belongs to several parents, then this will break again.

Take a look at the relative codex page for more information about this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.