Archived topic
Show specific text if fallback image is used instead featured image
11 replies · Started by Michael Alø-Nielsen on March 6, 2021
I'm trying to set up a header element which uses the featured image as a background image, if theres no featured image, the fallback image should be used.
However ...
When the featured image is used I'm outputting a custom field (with the photo credit) as text in the lower right corner and this works fine, but when the fallback image is used I want to output a specific photo credit text instead (not a custom field).
With the code I'm using both the custom field AND the specific credit text is shown when there is a featured image present. When no featured image is present only the specific credit text is shown (as it should).
This is what I've tried so far:
<?php $thumb_id = get_post_thumbnail_id( get_the_ID() ); if ($thumb_id = "90"): ?>
<p style="font-size:75%;float:right;margin-right:10px;margin-top:-20px;">
<span>Foto af <strong><a href="https://www.pexels.com/da-dk/@brett-sayles?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels">Brett Sayles</a></strong> fra <strong><a href="https://www.pexels.com/da-dk/foto/trae-kunst-vaeg-design-2850290/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels">Pexels</a></strong></span>
</p>
<?php else: ?>
<p style="font-size:75%;float:right;margin-right:10px;margin-top:-20px;"><span>{{custom_field.credit}}</span></p>
<?php endif; ?>
The site is not online, so I can't provide a link.
Hi there,
try this method:
<?php
$thumb_id = get_post_thumbnail_id( get_the_ID() );
if ($thumb_id):
?>
// if thumbnail is present return thumbnail caption here
<?php else: ?>
// return fallback caption here
<?php endif; ?>
Makes no difference.
The right caption is shown when there's no featured image.
Both captions are showing when there is a featured image.
Try checking first to see if the post has a thumbnail then run your conditions.
Something like this:
global $post;
if (has_post_thumbnail( $post->ID ) ){
get_the_post_thumbnail($post->ID);
// Return your featured omage caption here
} elseif {
// Return your fallback caption here
}
This gives an error. part of the php code gets printed
ID) ){ get_the_post_thumbnail($post->ID); ?>
along with both captions when there is a featured image
and the fallback caption if there's no featured image.
Hi there,
Can you share the full code that causes this?
Normally when a part of the code is printed out, it means there's a syntax issue. Usually related to delimiters.
Full code:
<?php global $post; if (has_post_thumbnail($post->ID) ){ get_the_post_thumbnail($post->ID); ?>
<p style="font-size:75%;float:right;margin-right:10px;margin-top:-20px;"><span>{{custom_field.credit}}</span></p>
<?php } elseif { ?>
<p style="font-size:75%;float:right;margin-right:10px;margin-top:-20px;">
<span>Foto af <strong><a href="https://www.pexels.com/da-dk/@brett-sayles?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels">Brett Sayles</a></strong> fra <strong><a href="https://www.pexels.com/da-dk/foto/trae-kunst-vaeg-design-2850290/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels">Pexels</a></strong></span>
</p>
<?php } ?>
Sorry about this - i just realised you're wanting to output this in a Header Element - which doesn't support PHP by default. Instead you would need to register a shortcode like this:
add_shortcode( 'featured_caption', 'db_featured_image_caption' );
function db_featured_image_caption() {
global $post;
ob_start();
if ( has_post_thumbnail( $post->ID )) {
$caption = get_post_meta(get_the_ID(), 'caption', true);
$content = '<p class="featured-caption">' . $caption . '</p>';
} else {
$content = '<p class="featured-caption"> The fallback caption here </p>';
}
ob_end_clean();
return $content;
}
Then you would add just the shortcode [featured_caption] in your Header Element.
It works fine for when there's no featured image present, but how do i get this:
<p style="font-size:75%;float:right;margin-right:10px;margin-top:-20px;"><span>{{custom_field.credit}}</span></p>
into the output for when a featured image is present?
This part of the code will automatically display the content of the caption custom field that you have set in the post editor:
if ( has_post_thumbnail( $post->ID )) {
$caption = get_post_meta(get_the_ID(), 'caption', true);
$content = '<p class="featured-caption">' . $caption . '</p>';
}
Where is the caption custom field being set ?
Bingo!
I just needed to put the name of my custom field (credit) instead of your word (caption).
Now it works just like I want. Thanks so much for your help.
Oops - my bad :) Glad to hear its working!