URGENT! NEW FEATURE NEEDED – A-Z by posts with Thumbnails
-
It is a nice plugin but we need a feature of posts in A to Z with Thumbnails.
- This topic was modified 6 years, 7 months ago by ephremfirew.
- This topic was modified 6 years, 7 months ago by ephremfirew.
-
You can add thumbnails by copying the template from
wp-content/plugins/a-z-listing/templates/a-z-listing.php
into your theme and editing accordingly using the normal WordPress functions to pull the featured image e.g.the_post_thumbnail()
.Since version 2.0.3,
the_post_thumbnail();
doesn’t work.
Can you help me ?This is a side-effect of my attempting to save a bit of memory and generation time. I purposely don’t load the full post object, so the thumbnail isn’t in memory.
Try with the following instead:
<?php $post = $a_z_query->get_the_item_object( 'I understand the issues!' ); if ( has_post_thumbnail( $post->ID ) ) : the_post_thumbnail( 'thumbnail' ); endif; ?>
The text
I understand the issues!
must be exactly that, to confirm that you do understand that on a large collection of posts you might hit slowness or memory usage problems.How is possible to output thumbs in this shortcode?
[a-z-listing post-type="product" taxonomy="brands" display="terms" numbers="before" grouping="numbers,3"]
- This reply was modified 6 years, 2 months ago by sroskylos.
To follow up on this issue, we’ve found that:
$post = $a_z_query->get_the_item_object( 'I understand the issues!' );
does not properly load the post object when querying custom post types (or maybe all post types.) So post thumbnails do not populate.In the method “get_the_item_object”, the second line –
$item = explode( ':', $this->current_item['item'], 1 );
doesn’t work where $this->current_item[‘item’] is a WP_Post object, and $item ends up being null, so nothing happens in the method.Short-circuiting the method by inserting the following at start of “get_the_item_object” fixes the method for CPTs at least:
global $post; $post = get_post( $this->current_item['item'] ); setup_postdata( $post ); return $post;
That’s a curious discovery, @tjebe, thanks for sharing. The format of the
$this->current_item['item']
value changed in 2.0.0 but I have just realised that if a site has hooked thea_z_listing_item_indices
filter then they might be returning the old style whereitem
was aWP_Post
orWP_Term
object.If you have such a hook implemented then you can immediately fix it by changing your filter function to something like:
add_filter( 'a_z_listing_item_indices', 10, 3 ); // the last number is different! function my_item_indices_hook( $indices, $item, $type ) { $indices = array(); // this empties the default index if ( 'terms' === $type ) { $title = $item->name; $item_id = $item->term_id; $permalink = get_term_link( $item ); } else { $title = get_the_title( $item ); $item_id = $item->ID; $permalink = get_the_permalink( $item ); } // replace the next line with your logic to determine the index letter $index = mb_substr( $title, 0, 1, 'UTF-8' ); $indices[ $index ][] = array( 'title' => $title, 'item' => ( 'terms' === $type ) ? "term:{$item_id}" : "post:{$item_id}", 'link' => $permalink, ); return $indices; }
I will also roll a new release to fix the situation where a site has customised the index returning a
WP_Post
object in the old style.Ah, that’s exactly what’s happening! We have a custom a_z_listing_item_indices filter in place. I added your fix to our filter, and that sets things up properly now.
The only other issue is that in “get_the_item_object”, the item is exploded out with the ‘:’ delimiter, but it’s set to return only 1 item in the array instead of the needed 2.
If you change the limit parameter to 2, everything works fine:
$item = explode( ':', $this->current_item['item'], 2 );
Thanks for the help.
I’m working on a 2.1.0 version which will add two new filters for overriding the index letters and titles of items. Rather than trying to memorise or copy my implementation of the heavily nested array structure, you will be able to instead hook the filters
a_z_listing_pre_index_item_title
anda_z_listing_item_index_letter
.Their implementation will be as follows:
add_filter( 'a_z_listing_pre_index_item_title', 'my_override_titles', 10, 3 ); function my_override_titles( $title, $item, $type ) { // $title is the title of the item as used for the index // set $title to your preferred format. This will then be indexed by the // default extraction - i.e. the first letter of the new title will be // the index letter used for this item // delete Mr and Mrs from the start of titles (presume they're names): $title = preg_replace( '^((Mr)|(Mrs))\s+', '', $title ); return $title; } add_filter( 'a_z_listing_item_index_letter', 'my_override_index_letter', 10, 3 ); function my_override_index_letter( $letters, $item, $type ) { // $letters is an array of letters to use for the item in the index // any item can be indexed under any number of letters if ( 15 === $item->ID ) { // let's index this item under P and Z. $letters = array( 'P', 'Z' ); } return $letters; }
- This reply was modified 6 years, 2 months ago by Dani Llewellyn. Reason: added example of overriding $title
- This reply was modified 6 years, 2 months ago by Steven Stern (sterndata).
Hello @diddledan,
I need to know if your 2.1.0 version is near to be ready please.
I tried @tjebe solution by adding $post = get_post( $this->current_item[‘item’] ); but I had no luck. I’m still getting NULL when calling the thumbnail.I do not have a custom index, just a custom post type template.
Thank you.
Version 2.1.0/2.1.1 is now available for download.
- The topic ‘URGENT! NEW FEATURE NEEDED – A-Z by posts with Thumbnails’ is closed to new replies.