WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Post Thumbnails
[resolved] Adding direct URL src to plugin with wp_get_attachment_image_src (12 posts)

  1. gabrielstuff
    Member
    Posted 1 year ago #

    Hi !

    I needed to be able to just fetch the src of the secondary image in a specific size.
    Thus I added that :

    /**
    		 *
    		 * @param string $post_type The post type.
    		 * @param string $id The id used to register the thumbnail.
    		 * @param int $post_id Optional. The post ID. If not set, will attempt to get it.
    		 * @param string $size Optional. Image size.  Defaults to 'thumbnail'.
    		 * @return src of the Thumbnail or false if the post doesn't have a thumbnail for the given post type, and id.
    		 */
    		public static function get_attachment_image_src($post_type, $id, $post_id = 0, $size = 'post-thumbnail') {
    			if (!$post_id) {
    				$post_id = get_the_ID();
    			}
    
    			$post_thumbnail_id = self::get_post_thumbnail_id($post_type, $id, $post_id);
    			$image = wp_get_attachment_image_src($post_thumbnail_id, $size);
    			if(is_array($image))
    				return $image[0];
    			else
    				return false;
    		}

    I'm happy with it ! Thanks for your work. Please do not hesitate to add this function to your next update.

    http://wordpress.org/extend/plugins/multiple-post-thumbnails/

  2. lord_dev
    Member
    Posted 1 year ago #

    You sir, are a gentleman and a scholar!

    Exactly what I was looking for, and may I suggest this is added to the next version? (I presume if there is an update, it will just override this code)

  3. gabrielstuff
    Member
    Posted 1 year ago #

    Yup ! upadte with modified core plugin file = code go away :)

    Actually the function is public, so we could try to make it update safe by overriding the function it self in a if statment.

    I'll test that later.

  4. charmedworks
    Member
    Posted 1 year ago #

    Hi, I would like the thumbnail to link to the large version. Can I use the code above to facilitate this and how would I do it?

    The featured image (regular thumbnail) can be referenced this way to link to the large version of the image:

    <?php
     if ( has_post_thumbnail()) {
       $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
       echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
       the_post_thumbnail('thumbnail');
       echo '</a>';
     }
     ?>
  5. charmedworks
    Member
    Posted 1 year ago #

    Also posted in the question about a different function get_post_thumbnail_url as I thought that might be able to be used to do this, but would prefer the method above if possible, not trying to overpost, thank you.

  6. charmedworks
    Member
    Posted 1 year ago #

    Hi, I found this:

    http://wordpress.org/support/topic/plugin-multiple-post-thumbnails-llink-to-full-size-image-from-thumbnail?replies=14

    Which I had missed earlier, and used code modified from here to make my link to larger images work. Thanks!

  7. Chris Scott
    Member
    Plugin Author

    Posted 1 year ago #

    @gabrielstuff thanks for this!

    Instead of adding a new function, I've updated get_post_thumbnail_url() so to add size as an optional parameter so the function signature will look like: get_post_thumbnail_url($post_type, $id, $post_id = 0, $size = null)

    This will be in version 1.5 which is coming out in the next few days.

  8. lord_dev
    Member
    Posted 1 year ago #

    Hi Chris Scott,

    Thanks for your great plugin.

    Have you updated this function yet: get_post_thumbnail_url($post_type, $id, $post_id = 0, $size = null)

    I have just updated your plugin to the latest version and I cannot get this to work just like the original function at the top of this page.

    Any help appreciated!

  9. Chris Scott
    Member
    Plugin Author

    Posted 1 year ago #

    Can you test this out with the following: https://raw.github.com/voceconnect/multi-post-thumbnails/master/multi-post-thumbnails.php

    Adds an optional $size parameter to get_post_thumbnail_url to pass in the thumbnail size.

  10. lord_dev
    Member
    Posted 1 year ago #

    Chris Scott - Works perfectly thanks!!

    When will you release the new code with 1.5?

    Cheers

  11. Chris Scott
    Member
    Plugin Author

    Posted 1 year ago #

    @lord_dev 1.5 should go out tomorrow.

  12. Chris Scott
    Member
    Plugin Author

    Posted 1 year ago #

    @lord_dev 1.5 was just released. If you have any issues, please update this thread.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic