WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom link for featured image (7 posts)

  1. jrcollins
    Member
    Posted 5 months ago #

    I've done a lot of searching but can't seem to find anything about how to do this. I want to create a custom field that will attach a link to the featured image.

  2. WPMU DEV
    Member
    Posted 5 months ago #

    Hi @jrcollins,

    I hope you are well today and thank you for your question.

    You can use this free plugin http://premium.wpmudev.org/forums/topic/how-can-i-add-an-external-url-link-to-a-featured-image#post-540709 to attach a custom link from custom field to the featured image.

    You can also use this paid plugin http://epicplugins.com/external-url-link-to-featured-images/ to achieve the same.

    Cheers.

  3. jrcollins
    Member
    Posted 5 months ago #

    Hi, thanks for your suggestion but I don't think that's quite what I'm looking for.

    That first plugin is for changing the post title permalink to a custom url and the second one "...allows you to set your post thumbnail (featured image) from an external URL without uploading the image to your WordPress media library."

    I want to have featured images link to a custom url. I thought it would be possible to do this by creating a custom field and then calling up that variable in the code for the featured image. Unfortunately I still have only a very basic knowledge of php and I'm not confident enough to attempt this on my own.

  4. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 5 months ago #

    Edit: NVM duplicate thread.

  5. WPMU DEV
    Member
    Posted 5 months ago #

    Hi @jrcollins,

    Thanks for reply.

    You can just create custom field having name/key external_link_key and display it using following code.

    <?php $key="external_link_key"; $single=1; $external_link = get_post_meta($post->ID, $key, $single); ?>  
    
    <?php if($external_link == ""){ ?>
    	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail(); ?></a> <?php ;}
    else{ ?>
    	<a href="<?php echo $external_link; ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail(); ?></a> <?php } ?>

    Best Regards,

  6. jrcollins
    Member
    Posted 5 months ago #

    Hi, thanks for your input. I found your code to be overly complicated and in the end used the following simpler code:

    <?php $name = get_post_meta($post->ID, 'ExternalUrl', true);
    if( $name ) { ?>
    <a href="<?php echo $name; ?>"><?php the_post_thumbnail(); ?></a>
    <?php } else {
    the_post_thumbnail();
    } ?>

    Thanks for your help!

  7. WPMU DEV
    Member
    Posted 5 months ago #

    Hi @jrcollins,

    You are welcome.

    The provided code looks complicated because i have declared function parameters as variables so that you can can change it, added link for normal thumbnails and also added title attribute to the anchor so that a tool tip will be displayed on hovering the link text but if you don't need this then you can remove it as you have done so.

    I liked that you have made code simpler.

    Cheers.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.