Image

  • 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?

  • Hi there,

    You can use the render_block filter 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.php or 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_ID with the ID of the post, and update the custom image URL.

  • 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

  • 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 $replacements arrays with your own.

  • Brilliant- thanks so much!!

  • I have closed topic now

  • Glad it worked, no problem!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.