-
MaBaBu
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 filewith 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/
-
MaBaBu
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 ….. ‘; -
Fernando
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.
-
MaBaBu
Hi Fernando,
Thanks for your quick reply
-
Fernando
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. -
MaBaBu
Thanks no change
-
Fernando
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.
-
MaBaBu
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.
-
MaBaBu
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.
- You must be logged in to reply to this topic.