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.

Query loop – default image or no image block if there is no featured image?

  • I have a lot of posts without a featured images and lot’s of just headline links to external URL’s
    How can I use a default image if there is no featured images or have no empty image block visible?

  • Hi there,

    You can use a function like this to add a fallback image when there’s no featured image attached to a post, make sure replace URL to your image file with the actual image URL.

    function add_fallback_featured_image( $post_id ) {
      if ( !has_post_thumbnail( $post_id ) ) {
        // Path to the fallback image
        $fallback_image_url = 'URL to your image file';
    
        // Create attachment post for fallback image
        $fallback_image_id = wp_insert_attachment( array(
          'post_title'     => basename( $fallback_image_url ),
          'post_content'   => '',
          'post_status'    => 'inherit'
        ), $fallback_image_url, $post_id );
    
        // Set fallback image as featured image
        set_post_thumbnail( $post_id, $fallback_image_id );
      }
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • Ying,
    Thank you so much for your quick reply.
    I can’t get it to work.
    – added image, added code into child theme – functions.php
    Replaced URL – ‘URL to your image ….. ‘;

  • Hello there,

    For reference, can you share admin login credentials in the Private Information field? We’ll try to take a closer look at why it isn’t working.

  • Hi Fernando,

    Thanks for your quick reply

  • Can you try creating a Hook Element with this code:

    <?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    } else { ?>
    <img src="URL to your image file" alt="<?php the_title(); ?>" />
    <?php } ?>

    Make sure to toggle on Execute PHP. Then. hook it to: generate_after_entry_title.

  • Thanks no change

  • Oh. May we know why you’re using a Content Template for single posts? If there’s no specific reason, you can just delete that element as it’s not meant for Single posts.

  • Fernando,
    Thanks again for your quick reply.
    I don’t understand what you mean “using a Content Template”.
    We do need a “Content Template”, the page has approx. 6k posts,
    I’m volunteering in migrating from a Typo3 page to GP/GB.
    80% of the posts are classic posts, with text and images, (some of the featured images got lost during the migration). 20% of the posts are links to external URL’s with some excerpts and no featured images.
    We are using “Page Links To” to link the Title to external URL’s.

    Sunny greetings from LA

  • I would recommend giving up the featured image in your design, as most of your posts don’t have a featured image, even if we use code to add a fallback image, it will result in almost all the featured images looking the same, it doesn’t make any sense.

  • Thanks
    I guess the author could use a custom “featured” image if he has no image at all.
    Not perfect but for all the new posts it could work.
    Thank again.

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