Multiple Featured Images Plugin

Hi

I'm trying to use the Multiple Featured Images plugin to add a second featured image on specific posts which when set should replace the original post thumbnail.

Not sure what's going wrong (and I've been through the plugin forum, etc) but I seem to be losing the original post thumbnails if a second image is not set, so can only assume my if/else statements are wrong?

See screenshot attached. Under Paintings only the post with a second featured image appears, whereas there should be 4 thumbnails as under Photography.

This is my code below, so if anyone has any suggestions, that would be much appreciated.

Thank you!

<?php if ( class_exists('MultiPostThumbnails')) { ?>
			<a class="gallery-img" href="<?php the_post_thumbnail_url(); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">
    			<div class="featured-thumb">
		    		<?php MultiPostThumbnails::the_post_thumbnail(
		        		get_post_type(),
		        		'secondary-image'
		    		); ?>
		    		<p class="featured-title"><?php the_title(); ?></p>
		    	</div>
		<?php } else { ?>
			<?php if ( has_post_thumbnail() ) { ?>
	    		<a class="gallery-img" href="<?php the_post_thumbnail_url(); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">
	    			<div class="featured-thumb">
	        			<?php the_post_thumbnail(); ?>
	        			<p class="featured-title"><?php the_title(); ?></p>
	        		</div>
	        	</a>
			<?php } ?>
		<?php } ?>
  • Dimitris

    Hey there Gavin Wilshen,

    hope you're doing good and thanks for reaching us! :slight_smile:

    It seems that your very first conditional is actually looking if the plugin is active, nothing more.
    So it displays the 'secondary-image' but never get inside the default the_post_thumbnail() block as it's inside the 'else' statement.

    In order to display both images, try the following snippet

    <?php if ( kdmfi_has_featured_image('secondary-image') ) { ?>
    	<a class="gallery-img" href="<?php kdmfi_get_featured_image_src( 'secondary-image', 'full' ); ?>">
    			<div class="featured-thumb">
        		<?php kdmfi_the_featured_image( 'secondary-image', 'full' ); ?>
        	</div>
      </a>
    <?php } ?>
    
    <?php if ( has_post_thumbnail() ) { ?>
    		<a class="gallery-img" href="<?php the_post_thumbnail_url(); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">
    			<div class="featured-thumb">
        			<?php the_post_thumbnail(); ?>
        		</div>
        	</a>
    <?php } ?>

    References:
    https://wordpress.org/plugins/multiple-featured-images/installation/
    https://wordpress.org/plugins/multiple-featured-images/faq/

    Hope that was some help, feel free to post back here if more assistance is required!
    Warm regards,
    Dimitris

  • Gavin Wilshen

    Thanks Dimitris!

    I thought it might be something along those lines.

    I've tried updating my code though and now I'm seeing no featured images at all. Am I missing something? Do I need to be using the filter as shown here: https://wordpress.org/plugins/multiple-featured-images/faq/?

    The only other code I've added to get the plugin working is this in my functions.php

    if (class_exists('MultiPostThumbnails')) {
        new MultiPostThumbnails(
            array(
                'label' => 'Secondary Image',
                'id' => 'secondary-image',
                'post_type' => 'painting'
            )
        );
    }

    Thanks again!

  • Adam Czajczyk

    Hello Gavin!

    I'm not sure if the code from your last post is necessary at all. It seems however that the filter code - the first one given on plugin's install page - is a requirement:

    https://wordpress.org/plugins/multiple-featured-images/installation/

    I tested the code given by Dimitris with this "filter code" and that seems to be working fine. My suggestion then would be to:

    1. Use the code provided by Dimitris to display those featured images
    2. replace the code in functions.php with the "filter code" from plugin's installation doc

    Please however pay attention to the image IDs: in "filter code" example there's the "ID" value in array which is "featured-image-2" while in your codes a "secondary-image" is used. That has to be adjusted to be the same.

    Best regards,
    Adam

  • Gavin Wilshen

    Thanks Adam, that works!

    I replaced the code in my functions.php with that filter and then updated the id in my theme to match.

    Final issue is that the second featured image url is outputting an empty href with this code:

    <a class="gallery-img" href="<?php kdmfi_get_featured_image_src( 'featured-image-2', 'full' ); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">

    Is there a syntax error somewhere?

    Here's what I get in the Inspector (see screenshot).

    Thanks again!

  • Gavin Wilshen

    Fixed it. Just needed: echo kdmfi_get_featured_image_src

    Final solution below. Thanks again for the help - I couldn't have done it without you WPMUdev!

    // add filter to functions.php registering a second featured image
    
    add_filter( 'kdmfi_featured_images', function( $featured_images ) {
            $args_1 = array(
                'id' => 'featured-image-2',
                'desc' => 'Your description here.',
                'label_name' => 'Featured Image 2',
                'label_set' => 'Set featured image 2',
                'label_remove' => 'Remove featured image 2',
                'label_use' => 'Set featured image 2',
                'post_type' => array( 'painting' ),
            );
    
            $featured_images[] = $args_1;
    
            return $featured_images;
        });
    
    // display a second featured image instead of original featured image in template file if one is set
    
    <?php if ( kdmfi_has_featured_image('featured-image-2') ) { ?>
    			<a class="gallery-img" href="<?php echo kdmfi_get_featured_image_src( 'featured-image-2', 'full' ); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">
    			<div class="featured-thumb">
    		    		<?php kdmfi_the_featured_image( 'featured-image-2', 'full' ); ?>
    		    		<p class="featured-title"><?php the_title(); ?></p>
    		    	</div>
    		  	</a>
    		<?php } else { ?>
    			<?php if ( has_post_thumbnail() ) { ?>
    	    		<a class="gallery-img" href="<?php the_post_thumbnail_url(); ?>" title="<?php echo get_post(get_post_thumbnail_id())->post_title; ?>">
    	    			<div class="featured-thumb">
    	        			<?php the_post_thumbnail(); ?>
    	        			<p class="featured-title"><?php the_title(); ?></p>
    	        		</div>
    	        	</a>
    			<?php } ?>
    		<?php } ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.