• Resolved pcgardner

    (@pcgardner)


    Some of my terms are in single quotes, to indicate that they are informal or deprecated expressions. At present these terms are listed separately at the end of the listing under ‘#’. Of course this is not helpful; they should be listed under the first actual letter. For example, ‘Clobber texts’ should be listed under ‘C’.

    I assume this needs a tweak to the alphabetization function. What is the best way to do this?

    Thanks for your help.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Dani Llewellyn

    (@diddledani)

    You can add a filter function that returns the correct alphabet letter. I’ve put some code below that should work for you if you copy it into your theme’s functions.php file – you’ll need version 3.0.0 of A-Z Listing, which was released an hour ago. I’ve also made a note to add this feature in a future version.

    add_filter( 'a_z_listing_item_index_letter', 'ignore_quotes_in_a_z_listing', 10, 3 );
    
    function ignore_quotes_in_a_z_listing( $indices, $item, $type ) {
        if ( 'term' === $type ) {
            $title = $item->name;
        } else {
            $title = get_the_title( $item );
        }
    
        if ( false === strpos( $title, '"' ) && false === strpos( $title, '\'' ) ) {
            return $indices;
        }
    
        return [ mb_substr( $title, 1, 1 ) ];
    }
    Thread Starter pcgardner

    (@pcgardner)

    Thank you very much for that code. It’s taken me a while to get around to implementing it, but I’ve now got it working. I had to make a few changes:

    add_filter( 'a_z_listing_item_index_letter', 'ignore_quotes_in_a_z_listing', 10, 3 );
    
    function ignore_quotes_in_a_z_listing( $indices, $item, $type ) {
        if ( 'terms' === $type ) {
            $title = $item->name;
        } else {
            $title = get_the_title( $item );
        }
        if ( ( 0 === strpos( $title, '"' ) ) || ( 0 === strpos( $title, '\'' ) ) ) {
    	return [ mb_substr( $title, 1, 1 ) ];
        } else {
    	return $indices;
        }
    }

    Notes

    [1] In line 2 of the function it must be ‘terms‘, not ‘term’; it took me a while to spot that!

    [2] I had to test the first character of $title (so 0, not false), because your code would have caused Land’s End to be alphabetized under ‘A’.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ignore single quotes when alphabetizing’ is closed to new replies.