[Resolved] Show specific text if fallback image is used instead featured image

Home Forums Support [Resolved] Show specific text if fallback image is used instead featured image

Home Forums Support Show specific text if fallback image is used instead featured image

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1684590
    Michael 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.

    #1685314
    David
    Staff
    Customer Support

    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; ?>
    #1685320
    Michael Alø-Nielsen

    Makes no difference.
    The right caption is shown when there’s no featured image.
    Both captions are showing when there is a featured image.

    #1685648
    David
    Staff
    Customer Support

    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
    }
    #1685723
    Michael Alø-Nielsen

    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.

    #1686059
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

    #1686884
    Michael Alø-Nielsen

    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 } ?>
    #1686941
    David
    Staff
    Customer Support

    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.

    #1687008
    Michael Alø-Nielsen

    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?

    #1687020
    David
    Staff
    Customer Support

    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 ?

    #1687025
    Michael Alø-Nielsen

    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.

    #1687056
    David
    Staff
    Customer Support

    Oops – my bad 🙂 Glad to hear its working!

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