Support » Plugin: A-Z Listing » A_Z by surname (second word of post title)

  • Resolved odrum

    (@odrum)


    Is there any way to make the A-Z hook a surname (second word of a post) instead of listing by first name?

    Great plugin, thank you!
    Appreciate any help 🙂

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

    (@diddledani)

    Yes, this is possible with the a_z_listing_item_indices filter, so something like the following in your functions.php should work:

    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 ( $item_type === 'post-type-we-want' ) {
            // pull the title and get the first letter of the second word
            $title_parts = explode( ' ', $item->post_title )
    
            // the second word is in $title_parts[1] so check it is there
            if ( isset( $title_parts[1] ) && $title_parts[1] ) {
                // cut the first letter out for our index
                $index = substr( $title_parts[1], 0, 1 );
                // return a new array with our new index letter instead of the
                // indices already discovered by the plugin
                return array( $index );
            }
        }
    
        // if we get here we didn't override the indices so return
        // those already discovered.
        return $indices;
    }
    Thread Starter odrum

    (@odrum)

    Hi Daniel,
    Thank you for the help. But… it’s saying there’s an ‘unexpected ‘if’ on line 10′.

    Any ideas or help much appreciated as this is beyond my knowledge.
    Thank you, M

    Plugin Author Dani Llewellyn

    (@diddledani)

    that’s because I made a mistake and you copied it into your implementation 🙂 The line with $title_parts = needs a semicolon on the end, i.e. an ;

    Thread Starter odrum

    (@odrum)

    Hey Daniel,
    Thanks for getting back to me.

    I added the semicolon as suggested but it’s still not working. I ran the code through php code checker and it’s still coming up with the same error “unexpected ‘if'”.

    Any ideas???
    thanks, M

    Plugin Author Dani Llewellyn

    (@diddledani)

    Can you share the full file that you added the code to via a paste service such as WPBin and paste the link here? I’m thinking there is something amiss with the interaction between pre-existing code and the code I suggested above.

    Thread Starter odrum

    (@odrum)

    http://wpbin.io/4ldrlv

    thanks for your time and efforts 🙂

    Plugin Author Dani Llewellyn

    (@diddledani)

    Hiya, I’ve made a very small modification to potentially fix it for you. The updated code is at http://wpbin.io/8zbsdw .

    Thread Starter odrum

    (@odrum)

    sadly no, it’s still not hooking the surname/second word.

    I’ll need to look in to it a bit more as the listing does need to be by surname.

    If you do have the time and the will to look further it would be appreciated but thank you for your time spent already 🙂
    M

    Plugin Author Dani Llewellyn

    (@diddledani)

    If you are using the code as-is from my wpbin paste then you’ll need to edit it very slightly before it will work for your posts. Specifically there is a bit in there which catches the type of post to determine whether to alter the indices. You need to replace 'post-type-we-want' with the slug of the post type where your names are stored. Alternatively if you want every post the listing displays anywhere on your site to be reworked to use the second word instead of the first for their index letter then you can remove lines 14 and 26 in the wpbin.

    (just to be clear, I’m referring to the wpbin I wrote at http://wpbin.io/8zbsdw)

    • This reply was modified 5 years, 9 months ago by Dani Llewellyn. Reason: remove double indefinite articles
    Plugin Author Dani Llewellyn

    (@diddledani)

    Are you still stuck? Can I help you any further?

    Thread Starter odrum

    (@odrum)

    Hi Daniel,
    Sorry I went quiet (juggling stuff). Yes, I am still stuck. I tried the last edit you gave me but still nothing.

    Due to other functionality I have now changed the WP theme I’m using (still Divi builder). Could using Divi builder be the cause of things not happening?

    This a-z on surname is the main thing wanted and it’s driving me nuts so any further help would be hugely appreciated 🙂 M

    Plugin Author Dani Llewellyn

    (@diddledani)

    Oh I think I see the mistake I made in my example code. I dun goof’d :-p I guess I didn’t understand my own work :-p

    I confused $item_type, which I’ve coded in the plugin to report either “taxonomy” or “posts”. In the example I coded for you I was using it as the post_type which is clearly wrong. <sheepishly:>I Sorry 🙁

    Let’s try again:

    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 ( 'post-type-we-want' === get_post_type( $item ) ) {
            // pull the title and get the first letter of the second word
            $title_parts = explode( ' ', $item->post_title );
    
            // the second word is in $title_parts[1] so check it is there
            if ( isset( $title_parts[1] ) && $title_parts[1] )  {
                // cut the first letter out for our index
                $index = substr( $title_parts[1], 0, 1 );
                // return a new array with our new index letter instead of the
                // indices already discovered by the plugin
                return array( $index );
            }
        }
    
        // if we get here we didn't override the indices so return
        // those already discovered.
        return $indices;
    }

    don’t forget to change the value 'post-type-we-want' for the slug of your post type, e.g. for the in-built default WordPress types: post for Posts or page for Pages.

    Thread Starter odrum

    (@odrum)

    Hi, Getting error message so to check I am doing this right…

    a) your code but replacing ‘post-type-we-want’ with ‘post’ (as I want to hook all posts)
    b) I am putting this in functions.php of my child theme

    thanks again 🙂

    Plugin Author Dani Llewellyn

    (@diddledani)

    Putting it in functions.php is correct. As it’s erroring then the code is at least being loaded. Is there any error message or log in your webserver’s error_log file which might illuminate the problem?

    Thread Starter odrum

    (@odrum)

    just thought I’d double check! Yes, this is the first time it’s done something!

    I am getting a screen full of this:
    Warning: array_merge_recursive(): Argument #2 is not an array in /home/buildodc/public_html/wp-content/plugins/a-z-listing/partials/class-a-z-listing.php on line 433

    … but right at the bottom of the page I am getting the a-z listing with 1 name (it is just a one name person so no first/surname).

    Argh……..

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘A_Z by surname (second word of post title)’ is closed to new replies.