WordPress.org

Ready to get started?Download WordPress

Forums

How to alphabetize list of tags which use special chracters? (5 posts)

  1. markellison
    Member
    Posted 3 years ago #

    This is a follow-on from this thread: http://wordpress.org/support/topic/how-to-display-the-number-of-posts-under-each-tag

    The website I produced is here:

    The background is that I've created a sort of index page which:

    1. Loops through all posts looking for tags (eg persons' names)
    2. Prints the tags in a list
    3. Next to each tag, prints a comma separated list of excerpts from each of the posts tagged with that tag

    The live page is here: http://thedublinreview.com/contributors/

    The code that produces this is here:

    <?php
    
    $tags_and_posts = $wpdb->get_results("
    	SELECT tr.object_id,p.post_excerpt,p.guid,t.name
    	FROM wp_term_relationships tr
    	JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    	JOIN wp_posts p ON tr.object_id = p.ID
    	JOIN wp_terms t ON tt.term_id = t.term_id
    	WHERE tt.taxonomy = 'post_tag'
    	AND p.post_status = 'publish'
    	AND p.post_type = 'post'
    	ORDER by t.name
    ");
    
    $tag_array = array();
    $letters = array();
    
    foreach( $tags_and_posts as $unwanted_key => $result ) {
    
    	$tag_array[$result->name][] = '<a href="'. get_permalink( $result->object_id ) .'">' . $result->post_excerpt. '</a>';
    }
    
    foreach( $tag_array as $tag => $post_excerpts ) {
    
    	// Ignore
    
    	$letter = ( strpos( $tag, ' ' ) ) ? explode( ' ',$tag ) : $tag;
    	if( is_array( $letter ) ) {
    		$letter = array_reverse( $letter );
    		$surname = strtolower( $letter[0] );
    		$letter = $letter[0]{0};
    	}
    	else {
    		$surname = strtolower( $letter );
    		$letter = $letter{0};
    	}
    	$letters[ $letter ][ $surname ] =  $tag . ' <td class="issue-numbers"> ' . implode( ' <span class="sep">/ </span>', $post_excerpts ) . ' </td> ';
    }
    
    ksort( $letters );
    
    foreach( $letters as $tag_letter => $tags ) {
    
    	// Ignore
    
    	ksort( $tags );
    
    	// Create a list for each letter
    	print '<table id="index-table"><tr><td class="author-name">' . implode( '</td></tr><tr><td class="author-name">', $tags ) . '</td></tr></table>';
    }
    ?>

    My problem is the name Philip Ó Ceallaigh. This name gets alphabetized under "C", which is incorrect. Equally incorrect, though slightly more acceptable, is how I have the name listed now: Philip ÓCeallaigh (without the space between Ó and Ceallaigh), however this name gets alphabetized at the very bottom of the list after the "Z". I'm happy to stick with Philip ÓCeallaigh, but is there any way to alter the code so that "Ó" is alphabetized just after "O" and before "P"?

  2. markellison
    Member
    Posted 3 years ago #

    An additional challenge is this: when I have two surnames that are the same my page is only listing the tag once. Any ideas?

  3. Mark / t31os
    Moderator
    Posted 3 years ago #

    Hi Mark,

    Apologies i didn't come back to you sooner, the issue here lies with the PHP functions and how they sort, unfortunately they won't sort correctly with anything not 0-9/a-z/A-Z as there's no real UTF character support..

    See:
    http://www.google.com/search?q=php+array+sort+utf

    There are some proposed solutions(click through the google results) but i've not tried any to know if they do work effectively/correctly (maybe try adopting a couple and see if they work?).

    Hope the above helps all the same... ;)

  4. Drawer
    Member
    Posted 3 years ago #

    I followed you guys in the other thread and here, and after Mark added that important <?php to the beginning, etc, I have copied that whole code you wrote, Mark, and it works perfectly.

    However, I want this index (really a list) to just go on one page, not appear on all of them. How do I do that? I can see Mark E. did that in his blog, but I don't know how.

    Thanks very much.

  5. markellison
    Member
    Posted 3 years ago #

    Hi Drawer, sorry for the very late reply!

    Can you clarify what you mean by the list appearing on a single page? The list I have here does indeed appear on a single page: https://thedublinreview.com/contributors/

Topic Closed

This topic has been closed to new replies.

About this Topic