Support » Plugin: A-Z Listing » A_Z by surname

Viewing 8 replies - 1 through 8 (of 8 total)
  • My code:

    add_filter( 'a_z_listing_item_indices', 'my_a_z_index_filter', 10, 3 );
    
    function my_a_z_index_filter( $indices, $item, $item_type ) {
        // make sure we're filtering the right post type
        if ( 'page' === get_post_type( $item ) ) {
            // pull the title and get the first letter of the second word
            $full_name = explode( ' ', $item->post_title );
    
            // the last word is in the last element of $title_parts so check it is there
            $last_name = array_pop( $full_name );
    
            // ensure we actually found a last name
            if ( $last_name )  {
                // cut the first letter out for our index
                $index = substr( $last_name, 0, 1 );
    
                // set up a new empty array
                $indices = array();
    
                // only the first names are left in $full_name. Join them together again
                $first_names = join( ' ', $full_name );
    
                // Now put the last name first and separate with a comma+space from the first names
                $formatted_name = "{$first_names} {$last_name}";
    
                // add the new name associated with the item into the new array we created
                $indices[ $index ][] = array(
                    'title' => $formatted_name,
                    'item' => $item,
                );
    
                // return a new array with our new index letter instead of the
                // indices already discovered by the plugin
                return $indices;
            }
        }
    
        // if we get here we didn't override the indices so return
        // those already discovered.
        return $indices;
    }
    
    
    Plugin Author Daniel Llewellyn

    (@diddledan)

    It looks like you’re changing the index letter only, not changing the layout of the title itself. You can now do this in a much simpler way by using the a_z_listing_item_index_letter filter:

    add_filter( 'a_z_listing_item_index_letter', 'change_a_z_index_letters', 10, 3 );
    function change_a_z_index_letters( $index_letters, $item, $type ) {
        // make sure we're filtering the right post type
        if ( 'page' === get_post_type( $item ) ) {
            // pull the title and get the first letter of the second word
            $full_name = explode( ' ', $item->post_title );
    
            // the last word is in the last element of $title_parts so check it is there
            $last_name = array_pop( $full_name );
    
            // ensure we actually found a last name
            if ( $last_name )  {
                // cut the first letter out for our index
                return substr( $last_name, 0, 1 );
            }
        }
    
        // if we get here we didn't override the indices so return
        // those already discovered.
        return $index_letters;
    }
    Plugin Author Daniel Llewellyn

    (@diddledan)

    It’s worth noting, also, that I have a premium extension for the A-Z Listing plugin that will handle names (proper nouns) at https://a-z-listing.com/product/proper-nouns-extension/. Please use whichever you would prefer; the code above should cover your use-case fine, so the extension isn’t necessary for you 🙂

    elroyski

    (@elroyski)

    Thank you Daniel for the answer. After adding the code, I get many error

    Warning: Invalid argument supplied for foreach() in /usr/home/public_html/wp-content/plugins/a-z-listing/src/Indices.php on line 106

    Plugin Author Daniel Llewellyn

    (@diddledan)

    aah, my mistake. the return needs to be an array. Change return substr( $last_name, 0, 1 ); to return array( substr( $last_name, 0, 1 ) ); and that should fix it.

    elroyski

    (@elroyski)

    Many thanks! Great plugin 🙂

    Just one more thing, Daniel 🙂 These pages under the # sign.
    How to fix it? I created a custom alphabet, but this not resolve problem.

    (Ł,Ć,Ö in surname)

    #
    Jacek Łukawski
    Jakub Ćwiek
    Mesut Özil
    Tomasz Łapiński

    Plugin Author Daniel Llewellyn

    (@diddledan)

    You might find those letters are “multibyte” characters according to the Unicode standard. If this is true, you can try using mb_substr where I used substr above to fix this. You will still need those letters to be included in the alphabet, which you can do by adding the alphabet="" attribute to the shortcode.

    The default alphabet is:

    alphabet="AÁÀÄÂaáàäâ,Bb,CÇcç,Dd,EÉÈËÊeéèëê,Ff,Gg,Hh,IÍÌÏÎiíìïî,Jj,Kk,Ll,Mm,Nn,OÓÒÖÔoóòöô,Pp,Qq,Rr,Ssß,Tt,UÚÙÜÛuúùüû,Vv,Ww,Xx,Yy,Zz"
    

    Works 🙂 Thank you for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.