• iamonstage

    (@iamonstage)


    How would I change the field from with the a-z is using?

    e.g. The default is to use the Title of the term. But I have a custom field called ‘surname’. How would I use the a-z on this field? Whist still displaying both Title and ‘surname’ on the results page.

    Thanks!

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

    (@diddledani)

    Hi,

    This is not a feature that the plugin anticipates, and so it might be complex to set up how you want it, or possibly not doable at all.

    I think you can possibly use either:

    • a_z_listing_item_index_letter to override the index letter, but leave the title unchanged, or
    • a_z_listing_pre_index_item_title to change the title

    For the first option something like this in your theme’s functions.php might work:

    <?php
    add_filter( 'a_z_listing_item_index_letter', 'override_a_z_index_letter' );
    function override_a_z_index_letter( $original_index, $item, $type ) {
        $surname = get_post_meta( $item->ID, 'surname', true );
        if ( $surname ) {
            return array( \A_Z_Listing\Strings::maybe_mb_substr( $surname, 0, 1 ) );
        }
        return $original_index;
    }

    For the second option, something like this in your theme’s functions.php might work:

    <?php
    add_filter( 'a_z_listing_pre_index_item_title', 'override_a_z_title' );
    function override_a_z_title( $original_title, $item, $type ) {
        $surname = get_post_meta( $item->ID, 'surname', true );
        if ( $surname ) {
            return $surname;
        }
        return $original_title;
    }
    Thread Starter iamonstage

    (@iamonstage)

    Thanks for you reply @diddledan
    I have the below code alternative. Is this OK and do you see any problems with it?

    add_filter('a_z_listing_pre_index_item_title', 'lf_replace_term_name_for_artist_surname', 10, 3);
    function lf_replace_term_name_for_artist_surname($title, $item, $type)
    {
    	$artistsurname = '';
    	if ($type == 'terms') {
    		$artistsurname = get_field('artist_surname', $item);
    	}
    	if ($artistsurname) {
    		return $artistsurname;
    	} else {
    		return $title;
    	}
    }
    add_filter('_a-z-listing-extract-item-indices', 'lf_revert_original_term_name', 10, 3);
    function lf_revert_original_term_name($item_indices, $item, $type)
    {
    	if ($type == 'terms') {
    		if ($item_indices) {
    			foreach ($item_indices as $key => $item_indice) {
    				$item_indices[$key][0]['title'] = $item->name;
    			}
    		}
    	}
    	return $item_indices;
    }
    Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    It looks ok from a quick review; although, you should be aware that the _a-z-listing-extract-item-indices filter is meant for internal use so it is not guaranteed to function the same way in future versions.

    You might be able to achieve the same effect (to revert the title when you’ve overridden it for indexing) with better forward compatibility guarantees by copying the template into your theme and modifying line 57-59 to something like this:

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php
    	if ( 'term' === $a_z_query->get_the_item_type() ) {
    		$term = get_term( $a_z_query->get_the_item_id() );
    		echo apply_filters( 'term_name', $term->term_name );
    	} else {
    		$a_z_query->the_title();
    	}
    	?>
    </a>

    The default template is at wp-content/plugins/a-z-listing/templates/a-z-listing.php.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change the field from which is used’ is closed to new replies.