Multiple Post Thumbnails
[resolved] Finding position of attachment in array (5 posts)

  1. ssmithalignsoftcom
    Posted 4 years ago #

    I'm using the Multiple-post-thumbnail plugin in order that I can use one of two featured images for display on the post itself, and the second for an image slider on the home page which links to the post.

    The stock featured image is always the first attachment in the array, but the second image doesn't have a fixed position in the array, and I need to extract it but can't figure out how to do so. Below is the code which currently uses the image at index 0 of the array if there's no secondary-image, and the image at index 1 if there is. I need this to use the secondary-image if there is one, and the image at index 0 if there's not.

    Anyone offer any assistance on this?

    Thanks very much in advance,

    		$args = array(
    			'post_type' => 'attachment',
    			'numberposts' => -1, // bring them all
    			'post_status' => null,
    			'post_parent' => $post->ID
    		$attachments = get_posts($args);
    		if ($attachments) {
    			echo "<li>";
    			if (class_exists('MultiPostThumbnails')&& MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image')) {
    				$attachment=$attachments[1]; // second one
    			} else {
    				$attachment=$attachments[0]; // first one
    			echo "<a href='".get_permalink()."'>";
    			echo wp_get_attachment_image($attachment->ID,'Full');
    			echo "</a>";
    			echo "</li>";
    	//Reset Query


  2. Chris Scott
    Plugin Author

    Posted 4 years ago #

    Hi Steve,

    Give something like this a shot:

    $thumb_post_id = false;
    if (class_exists('MultiPostThumbnails')) {
    	$thumb_post_id = MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image'));
    } else {
    	$thumb_post_id = get_post_thumbnail_id();
    if ($thumb_post_id && $thumb_post = get_post($post_id)) {
    	echo "<a href='". esc_url(get_permalink($thumb_post->ID))."'>";
    	echo wp_get_attachment_image($thumb_post->ID,'Full');
    	echo "</a>";
    	echo "</li>";
  3. ssmithalignsoftcom
    Posted 4 years ago #

    Hi Chris,

    That works like a charm! Thanks very much!

    Just a note that there's an extra closing bracket at the end of line 3:

    $thumb_post_id = MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image'));

    should be

    $thumb_post_id = MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image');

    Really appreciate the help!

  4. Oken
    Posted 3 years ago #

    Hi Steve,

    I am currently using the Multiple-post-thumbnail plugin in order to also have one featured image thumbnail for my blog loop and one separate image for a slider.

    I'm currently looking for a slider that can integrate well with the Multiple-post-thumbnail plugin. I was wondering what slider you are using for your theme.

    If anyone has any possible slider suggestions, I would greatly appreciate it as well.

    Thank you in advance!

  5. ssmithalignsoftcom
    Posted 3 years ago #

    Hi Oken,

    For this site we're using a custom implementation of Alen Grakalic's Easy Slider plugin:



Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Multiple Post Thumbnails
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic