WordPress.org

Ready to get started?Download WordPress

Forums

How to make Tags appear in the order they were given instead of the ABC order? (12 posts)

  1. TransPersonal
    Member
    Posted 4 years ago #

    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?

  2. tomontoast
    Member
    Posted 4 years ago #

    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.

  3. TransPersonal
    Member
    Posted 4 years ago #

    Hi tomontoast,

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

  4. TransPersonal
    Member
    Posted 4 years ago #

    Anyone?

  5. tomontoast
    Member
    Posted 4 years ago #

    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!

  6. TransPersonal
    Member
    Posted 4 years ago #

    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

  7. tomontoast
    Member
    Posted 4 years ago #

    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);
    }
  8. TransPersonal
    Member
    Posted 4 years ago #

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

    <?php the_better_tags(); ?>

    no tags show at all...

  9. tomontoast
    Member
    Posted 4 years ago #

    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);
    }
  10. TransPersonal
    Member
    Posted 4 years ago #

    Sorry tomontoast but still nothing shows...

  11. tomontoast
    Member
    Posted 4 years ago #

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

  12. TransPersonal
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.