• Hi guys,

    If I have a post about zoos and I decide to give it the tags:

    zoos, animals

    in that order (writing zoos first and animals second), on the post it will still appear as “animals, zoos”. The reason why I want the tags to be listed in the order they were given instead of the default ABC order, is because I tend to give the most relevant tags at first followed by less relevant tags.

    Is there any way to make tags list according to the order they were given?

Viewing 11 replies - 1 through 11 (of 11 total)
  • You can’t do this easily because of the way the tags are generated, it would mean writing a custom function. I will suggest a patch to go into the core files to make ordering by a different value possible.

    Thread Starter transpersonal

    (@transpersonal)

    Hi tomontoast,

    Any idea what a functions.php hack for this would be? I don’t know PHP so I’m lost here.

    Thread Starter transpersonal

    (@transpersonal)

    Anyone?

    I’ve spent a while developing this:
    In functions.php add the following:

    function get_the_better_term_list( $id = 0, $taxonomy = 'post_tag', $before = '', $sep = '', $after = '' ) {
    	$id = (int) $id;
    	if ( ! $id && ! in_the_loop() )
    		return false;
    	if ( !$id )
    		$id = (int) $post->ID;
    	$terms = wp_get_object_terms($id, $taxonomy, array('orderby' => 'term_order'))
    	if ( is_wp_error( $terms ) )
    		return $terms;
    	if ( empty( $terms ) )
    		return false;
    	foreach ( $terms as $term ) {
    		$link = get_term_link( $term, $taxonomy );
    		if ( is_wp_error( $link ) )
    			return $link;
    		$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
    	}
    	$term_links = apply_filters( "term_links-$taxonomy", $term_links );
    	return $before . join( $sep, $term_links ) . $after;
    }
    function the_better_tags( $before = 'Tags: ', $sep = ', ', $after = '' ){
    	get_the_better_term_list(0, 'post_tag', $before, $sep, $after)
    }

    and replace all the_tags() in your theme with the_better_tags()

    Hope that works well for you!

    Thread Starter transpersonal

    (@transpersonal)

    Thank You So Much tomontoast!!!!

    I really appreciate you taking the time to write that code, if it works it will be immensely helpful. At the moment I’m having a little trouble, adding the code within the loop of functions.php gives the following error:

    Parse error: syntax error, unexpected T_IF

    Oh bugger I copied the old version not the new which I tested. Oops very sorry!
    Try

    function get_the_better_term_list( $id = 0, $taxonomy = 'post_tag', $before = '', $sep = '', $after = '' ) {
    	$id = (int) $id;
    	if ( ! $id && ! in_the_loop() )
    		return false;
    	if ( !$id )
    		$id = (int) $post->ID;
    	$terms = wp_get_object_terms($id, $taxonomy, array('orderby' => 'term_order'));
    	if ( is_wp_error( $terms ) )
    		return $terms;
    	if ( empty( $terms ) )
    		return false;
    	foreach ( $terms as $term ) {
    		$link = get_term_link( $term, $taxonomy );
    		if ( is_wp_error( $link ) )
    			return $link;
    		$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
    	}
    	$term_links = apply_filters( "term_links-$taxonomy", $term_links );
    	return $before . join( $sep, $term_links ) . $after;
    }
    function the_better_tags( $before = 'Tags: ', $sep = ', ', $after = '' ){
    	get_the_better_term_list(0, 'post_tag', $before, $sep, $after);
    }

    Thread Starter transpersonal

    (@transpersonal)

    This time there is no error but after changing the template tag to:

    <?php the_better_tags(); ?>

    no tags show at all…

    Oops another thing I forgot try the following, it will work this time, I forgot echo!

    function get_the_better_term_list( $id = 0, $taxonomy = 'post_tag', $before = '', $sep = '', $after = '' ) {
    	$id = (int) $id;
    	if ( ! $id && ! in_the_loop() )
    		return false;
    	if ( !$id )
    		$id = (int) $post->ID;
    	$terms = wp_get_object_terms($id, $taxonomy, array('orderby' => 'term_order'));
    	if ( is_wp_error( $terms ) )
    		return $terms;
    	if ( empty( $terms ) )
    		return false;
    	foreach ( $terms as $term ) {
    		$link = get_term_link( $term, $taxonomy );
    		if ( is_wp_error( $link ) )
    			return $link;
    		$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
    	}
    	$term_links = apply_filters( "term_links-$taxonomy", $term_links );
    	return $before . join( $sep, $term_links ) . $after;
    }
    function the_better_tags( $before = 'Tags: ', $sep = ', ', $after = '' ){
    	echo get_the_better_term_list(0, 'post_tag', $before, $sep, $after);
    }
    Thread Starter transpersonal

    (@transpersonal)

    Sorry tomontoast but still nothing shows…

    That would suggest you have no tags for that post. Otherwise try and add some debug echoes in to see whats going wrong.

    Thread Starter transpersonal

    (@transpersonal)

    Thanks for all your help tomontoast, but the code just won’t work. And I do have tags on every single post on my site. Anyway don’t worry about it, I found a core-hack solution:

    In wp-includes/taxonomy.php there is this line:

    $defaults = array(‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘fields’ => ‘all’);

    I just changed the ‘orderby’ from ‘name’ to ‘term_order’. And it works – on single posts the ordering is based on the order the tags were given. However on the home page the ordering is as was before (ABC). Oh well, it’s more important that the non-ABC order be on the post pages anyway, Guess you can’t have it all ??

    Thanks again.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to make Tags appear in the order they were given instead of the ABC order?’ is closed to new replies.