WordPress.org

Ready to get started?Download WordPress

Forums

Making Featured " Post Thumbnails" Images Have Url Links?? (5 posts)

  1. Shadowz
    Member
    Posted 3 years ago #

    I was trying to figure out how to put url links on the featured images "post thumnails", so when you click on the image it takes you to what ever the link is.

    Doesn't look like they exactly have a way to do it easily or maybe? So im guessing i would need to add some code of some sort?

    But i would like for to be as easy as putting the link in a custom field. So for every post i have a featured image on, it can have a different link and no hassle to change.

    I don't really want any HARD code links. Would like to be able to edit it in the wp-admin.

    Hope some one can help

    Thanks.

  2. Curtiss Grymala
    Member
    Posted 3 years ago #

    There isn't a best-practice way to do this, as far as I know. However, there are a few tricks you could use to get the desired end result.

    Two tips to help steer you in the right direction:
    1) Each featured image is stored in the database as an attachment to the related post.
    2) The "Caption" field is stored in the "post_excerpt" column for the attachment and the "Description" field is stored in the "post_content" column.

    What I would suggest would be to use either the "Caption" field or the "Description" field to store the URL, then, in your theme, use the get_post_thumbnail_id() function to pull the featured image's ID, the get_post() function (inserting the ID that's returned by the previous function) to get the full information about the image, then output either the post_excerpt (the caption) or post_content (the description) property of the object that's returned by the get_post() function.

    Hope that helps. Hopefully a better solution will come down the pipeline eventually.

  3. Shadowz
    Member
    Posted 3 years ago #

    Hey thanks

    Don't know to much about php, but i get the jest of what you mean, if you could help me figure out how to put that code in my header where it displays the featured image that would be grateful.

    here the part of the code that displays my featured image

    if ( has_post_thumbnail( $post->ID )  &&
    
    ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
    
    $image[1] >= HEADER_IMAGE_WIDTH ) :
    
    echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else : ?>
    
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
    
    <?php endif; ?>
  4. Curtiss Grymala
    Member
    Posted 3 years ago #

    This should work. It does the same thing your code above was doing; but this code checks to see if either the "Caption" or "Description" field starts with "http", then uses that as a link to wrap around the thumbnail if it does.

    <?php
    	if( has_post_thumbnail( $post->ID ) &&
    		$image = /* $src, $width, $height */ wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
    		$image[1] >= HEADER_IMAGE_WIDTH ) {
    
    		/* Retrieve the full "post" object for the featured image */
    		$thumb = get_post( get_post_thumbnail_id( $post->ID ) );
    
    		/* Check to see if the caption or description is a URL */
    		if( !empty( $thumb->post_content ) && substr( $thumb->post_content, 0, strlen('http') ) == 'http' ) { /* The "Description" field is a URL */
    			$thumblink = $thumb->post_content;
    		} elseif ( !empty( $thumb->post_excerpt ) && substr( $thumb->post_excerpt, 0, strlen('http') ) == 'http' ) { /* The "Caption" field is a URL */
    			$thumblink = $thumb->post_excerpt;
    		} else {
    			$thumblink = NULL; /* Set this to anything else if you want the header image to link to a default item when no URL is provided in the "Caption" or "Description" section */
    		}
    
    		/* Echo either the linked featured image or the regular featured image, depending on the value of the $thumblink variable */
    		echo ( !empty( $thumblink ) ) ? '<a href="' . $thumblink . '">' . get_the_post_thumbnail( $post->ID, 'post-thumbnail' ) . '</a>' : get_the_post_thumbnail ( $post->ID, 'post-thumbnail' );
    
    	} else { /* Output the default header image if the requirements for a thumbnail were not met above */
    ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
    <?php
    	}
    ?>
  5. davidsanger
    Member
    Posted 3 years ago #

    This new plugin allows you to define custom fields like Photographer and Image URL which might help

    Media Custom Fields

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.