Home › Forums › Pro Support › filter to alter output
Tagged: filters, generate_featured_image_output
- This topic has 4 replies, 2 voices, and was last updated 3 years, 3 months ago by
Xavier.
-
AuthorPosts
-
August 25, 2020 at 1:52 am #18578
Xavier
ParticipantHello There!
I’d like to know if is there a filter like «generate_featured_image_output» in WP Show Posts so I could be able to customize and put conditions on the output of images but in the home page.
I’ve already done so in the posts/archive pages but it doesn’t wor at the home.Thanks!
August 25, 2020 at 3:08 pm #18590Tom
KeymasterHi there,
WPSP doesn’t have a similar filter at the moment, unfortunately.
What exactly are you trying to do? There may be another option.
August 26, 2020 at 6:19 am #18615Xavier
ParticipantHi Tom!
Thanks very much for your quick answer.
Well, I’ve used this code in the archive pages which check if an image file is loaded and if is so, uses it as the thumbnail img:function etdla_custom_featured_image( $output ) { if ( get_post_meta( get_the_ID(), 'imatge_destacada_alternativa', true ) ) { $foto_alt_id = get_post_meta( get_the_ID(), 'imatge_destacada_alternativa', true ) ; $foto_alt_url = wp_get_attachment_url( $foto_alt_id ); $image_alt = get_post_meta($foto_alt_id, '_wp_attachment_image_alt', TRUE); return '<div class="post-image"> <a href="'. get_the_permalink( get_the_ID() ) .'"> <img width="300" height="190" src="'.$foto_alt_url.'" class="attachment-300x190x1 size-300x190x1 wp-post-image" alt="'.$image_alt.'" itemprop="image" /> </a> </div>'; } else { return $output; } } add_filter( 'generate_featured_image_output', 'etdla_custom_featured_image');
August 26, 2020 at 2:46 pm #18629Tom
KeymasterYou may be able to filter the WordPress featured image ID before it ever reaches the plugin or theme:
add_filter( 'get_post_metadata', 'tu_override_featured_image_id', 100, 4 ); function tu_override_featured_image_id( $metadata, $object_id, $meta_key, $single ) { if ( isset( $meta_key ) && 'thumbnail_id' == $meta_key ){ remove_filter( 'get_post_metadata', 'tu_override_featured_image_id', 100 ); $new_id = get_post_meta( $object_id, 'imatge_destacada_alternativa', true ) ; add_filter( 'tu_override_featured_image_id', 'getqtlangcustomfieldvalue', 100, 4 ); if ( $new_id ) { $metadata = $new_id; } } return $metadata; }
Not sure if it will work, but worth a shot.
August 27, 2020 at 4:17 am #18643Xavier
ParticipantHello again,
Well, nice try, Thanks. I’ve been playing a while and found a couple of things:
One: your code had two errors. The working one (errors in bold) is this:add_filter( 'get_post_metadata', 'tu_override_featured_image_id', 999, 4 ); function tu_override_featured_image_id( $metadata, $object_id, $meta_key, $single ) { if ( isset( $meta_key ) && '<strong>_thumbnail_id</strong>' == $meta_key ){ remove_filter( 'get_post_metadata', 'tu_override_featured_image_id', 100 ); $new_id = get_post_meta( $object_id, 'imatge_destacada_alternativa', true ) ; add_filter( <strong>'get_post_metadata'</strong>, 'getqtlangcustomfieldvalue', 100, 4 ); if ( $new_id ) { $metadata = $new_id; } } return $metadata; }
Two: Actually, this changes the featured image everywhere, while I need to swap it only in the «index» pages as in here:
https://tempsarts.cat/seccio/didactica-de-lart/
and in here ( which is the place I can’t… 😀 )
https://tempsarts.cat/
You can see the different thumbnails in the post ‘«Ferro i cànem» de Paco Ferrando‘.
Any further idea?
Again, thanks for your interest, Tom. -
AuthorPosts
- You must be logged in to reply to this topic.