-
Anonymous
Hi, how can I replace the featured image for a specific post with a custom image URL everywhere on my website except on the single post page itself?
-
George
Hi there,
You can use the
render_blockfilter to replace the featured image for a specific post everywhere except on the single post page itself.Add this to your child theme’s
functions.phpor a code snippets plugin:add_filter( 'render_block', function( $block_content, $block ) { if ( empty( $block['blockName'] ) || $block['blockName'] !== 'generateblocks/container' ) { return $block_content; } $post_id = YOUR_POST_ID; // Replace with your post ID $thumbnail_id = get_post_thumbnail_id( $post_id ); $thumbnail_url = wp_get_attachment_url( $thumbnail_id ); if ( ! $thumbnail_url || strpos( $block_content, $thumbnail_url ) === false ) { return $block_content; } if ( get_the_ID() !== $post_id || is_single( $post_id ) ) { return $block_content; } $new_url = 'https://example.com/your-custom-image.jpg'; // Replace with your custom image URL return str_replace( $thumbnail_url, $new_url, $block_content ); }, 10, 2 );Replace
YOUR_POST_IDwith the ID of the post, and update the custom image URL. -
Anonymous
Thank you- I noticed it doesn’t apply to search- when I search for a keyword it shows the old photo. Is there an updated version to apply to this so the image is updated everywhere except for single page.
Also, I need to do this for more than one post. Is there a more efficient way to handle multiple posts than adding a separate snippet for each one?
Thanks in advance
-
George
Hi there,
The search results use a different template to display the featured image, so you’ll need a second filter to cover that. Here’s an updated version that handles both and supports multiple posts:
// For Loop Templates (background images in GB containers) add_filter( 'render_block', function( $block_content, $block ) { if ( empty( $block['blockName'] ) || $block['blockName'] !== 'generateblocks/container' ) { return $block_content; } $replacements = array( 123 => 'https://example.com/custom-image-1.jpg', 456 => 'https://example.com/custom-image-2.jpg', ); $current_id = get_the_ID(); if ( ! isset( $replacements[ $current_id ] ) || is_single( $current_id ) ) { return $block_content; } $thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( $current_id ) ); if ( ! $thumbnail_url || strpos( $block_content, $thumbnail_url ) === false ) { return $block_content; } return str_replace( $thumbnail_url, $replacements[ $current_id ], $block_content ); }, 10, 2 ); // For default blog/search output add_filter( 'post_thumbnail_html', function( $html, $post_id ) { $replacements = array( 123 => 'https://example.com/custom-image-1.jpg', 456 => 'https://example.com/custom-image-2.jpg', ); $post_id = absint( $post_id ); if ( ! isset( $replacements[ $post_id ] ) || is_single( $post_id ) ) { return $html; } $thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( $post_id ) ); if ( ! $thumbnail_url ) { return $html; } return str_replace( $thumbnail_url, $replacements[ $post_id ], $html ); }, 10, 2 );Replace the post IDs and URLs in the
$replacementsarrays with your own. -
Anonymous
Brilliant- thanks so much!!
-
Anonymous
I have closed topic now
-
George
Glad it worked, no problem!
- You must be logged in to reply to this topic.