- This topic has 11 replies, 3 voices, and was last updated 3 years ago by David.
-
AuthorPosts
-
March 6, 2021 at 7:38 am #1684590Michael Alø-Nielsen
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.
March 7, 2021 at 5:35 am #1685314DavidStaffCustomer SupportHi 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; ?>
March 7, 2021 at 5:43 am #1685320Michael Alø-NielsenMakes no difference.
The right caption is shown when there’s no featured image.
Both captions are showing when there is a featured image.March 7, 2021 at 8:38 am #1685648DavidStaffCustomer SupportTry 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 }
March 7, 2021 at 9:53 am #1685723Michael Alø-NielsenThis 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.March 7, 2021 at 6:10 pm #1686059ElvinStaffCustomer SupportHi 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.
March 8, 2021 at 8:04 am #1686884Michael Alø-NielsenFull 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 } ?>
March 8, 2021 at 8:35 am #1686941DavidStaffCustomer SupportSorry 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.March 8, 2021 at 9:15 am #1687008Michael Alø-NielsenIt 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?March 8, 2021 at 9:25 am #1687020DavidStaffCustomer SupportThis 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 ?
March 8, 2021 at 9:30 am #1687025Michael Alø-NielsenBingo!
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.March 8, 2021 at 9:41 am #1687056DavidStaffCustomer SupportOops – my bad 🙂 Glad to hear its working!
-
AuthorPosts
- You must be logged in to reply to this topic.