Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Placeholder images in grid

  • Is there a way to automatically put a placeholder image in the grid for posts with no featured image?

  • Hi there,

    1. You can create a block element – content template for the CPT archive, then use the image block of GenerateBlocks plugin to pull the featured image.

    2. Add a CSS class to the image block, eg. payment-featured-image.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    Screenshot for your reference: https://app.screencast.com/W6b6K5S5ChUL6

    3. Add this PHP to add a fallback image as the placeholder when there’s no featured image, change 100 to the actual fallback image ID which can be found in the image editor’s URL.

    add_filter( 'generateblocks_dynamic_image_fallback', 'custom_dynamic_image_fallback', 10, 3 );
    
    function custom_dynamic_image_fallback( $featured_image_id, $attributes, $block ) {
        if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'payment-featured-image' ) !== false ) {
    	$featured_image_id = get_post_thumbnail_id(get_the_ID());
            $fallback_image_id = '100'; 
            if ( $featured_image_id ) {
                return $featured_image_id;
            }
        }
    
        return $fallback_image_id;
    }
  • Thanks is there not a way to use hooks rather than installing blocks and going down that route? My grid is just using the one that comes with the standard generatepress plugin

  • Try this PHP, change 5 to the fallback image ID.

    function custom_fallback_featured_image() {
        // Check if the post has a featured image
        if ( has_post_thumbnail() ) {
            return;
        } else {
            // If no featured image, use fallback image with ID 5
            $fallback_image_id = '5';
            $fallback_image = esc_url( wp_get_attachment_image_url( $fallback_image_id, 'full' ) );
            
            // Generate the output with the featured image URL
            $output = printf(
                '<div class="post-image">	
                    <a href="%1$s">
                        <img src="%2$s" alt="Fallback Image">
                    </a>
                </div>',
                esc_url( get_permalink() ),
                $fallback_image
            );
            return $output;
        }
    }
    
    add_action( 'generate_after_entry_header', 'custom_fallback_featured_image' );
    
  • fantastic, thank you, i added is_archive() so it only ran on the archives

  • You are welcome   🙂

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