Using wp_get_attachment_image for thumbnails and external link (3 posts)

  1. solidhex
    Posted 7 years ago #

    Hi all

    I've created a function to place a random thumbnail image on a page of a client's website. The function looks like:

    function inspired_by_image ($size='thumbnail')
    	$attachments =& get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand'));
    	if (!empty($attachments)) {
    		$first_attachment = array_shift($attachments);
    	echo wp_get_attachment_image($first_attachment->ID, $size);

    It's working fine, excerpt for that the client would like to add an _external_ link to the image, as opposed to linking to the image itself - e.g. the image links to http://www.google.com.

    I noticed the Link URL field in the image uploader section ignores manually typed in links that are not on the site. Do i need to create a separate field in the database to pull this off?

  2. solidhex
    Posted 7 years ago #

    A simple workaround to this is to add the link to the description field.

  3. Endolil
    Posted 6 years ago #

    that's exactly with what I ended up.
    Only a workaround because the URL field is not made for optional links.

Topic Closed

This topic has been closed to new replies.

About this Topic