• Hi,

    I love your plugin, it’s exactly what I need, but unfortunately all my post titles have the same format which means everything gets filed under “h”.

    This could be solved easily by alphabetising by slug rather than post title, but I simply cannot find how to do that, even though I’m sure it can’t be very difficult…

    Your help is much appreciated!

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

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

    (@diddledani)

    This requires a bit of custom PHP to hook the filter a-z-listing-item-index-letter

    Something like this might work:

    add_filter( 'a-z-listing-item-index-letter', 'override_a_z_index_letter', 10, 2 );
    function override_a_z_index_letter( $previous_indices, $item ) {
        return array( mb_substr( $item->post_name, 0, 1, 'UTF-8' ) );
    }
    Thread Starter kes2019

    (@kes2019)

    Thanks!

    So now it alphabetises by slug (post_name), which is great, but I would also love for it to display only the post_name, and not the entire title of the post. Is that possible?

    Thanks for all your help!

    Thread Starter kes2019

    (@kes2019)

    Sorry to be a bother, but is my problem fixable? If it’s not, that’s fine, then I’ll look for another solution…

    So the problem is that I would like to alphabetise by slug but also only display the slug in the alphabetic list. You gave me some code for the first part but now it still displays the whole title.

    Thanks in advance for your help!

    Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    Sorry I lost track of this thread. The title can be manipulated with the the_title filter, which is a WordPress default thing; or you can use a custom A-Z Listing template page by copying the file from wp-content/plugins/a-z-listing/templates/a-z-listing.php into your theme alongside the style.css. In the custom template you will want to replace:

    <?php $a_z_listing->the_title(); ?>
    

    With something like this:

    <?php
    $title = $a_z_listing->get_the_title();
    $post = $a_z_listing->get_the_item_object( 'I understand the issues!' );
    $title = preg_replace( '/^' . $post->post_name . '/i', $title );
    echo $title;
    ?>

    Note the text 'I understand the issues!' must state exactly that, and indicates that you’re aware that using the feature on larger listings (more posts) may be slow or break the page entirely with memory exhaustion or timeouts.

    If you go the the_title route you want to do something like this in your functions.php file:

    add_filter( 'the_title', 'override_a_z_titles', 10, 2 );
    function override_a_z_titles( $title, $post_id ) {
        if ( ! is_page( $page_id_or_title_or_slug_of_a_z_listing_page ) ) {
            return $old_title;
        }
        $post = get_post( $post_id );
        $title = preg_replace( '/^' . $post->post_name . '/i', '', $title );
        return $title;
    }

    The big caveat is I have written this from memory, without double checking that either example is correctly coded.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Alphabetise by slug?’ is closed to new replies.