WordPress.org

Ready to get started?Download WordPress

Forums

Featured Image Link (20 posts)

  1. arcum
    Member
    Posted 4 years ago #

    Hi,

    I was wondering, when I make a new post with a featured image attached to it, I can make the image clickable with a url of my choice.
    So, now I want my featured posts on my frontpage to link directly to the url I've attached to my image (of that featured post)...
    Because right now people have got to click on the image, going to the post, and have to click again on that image to go to the site mentioned.

    Check my site to help visualize things a little; http://www.mtgfeeds.com - eg. featured post = mananation's - when you click on the image while on the frontpage - you directly get redirected to mananation.com

    Anyone care to help me out here?

    I would be mighty grateful... :)

    Kind regards

  2. MartinLeclercq
    Member
    Posted 3 years ago #

    no, i'm having the same question. Been trying different approaches, but no luck...

  3. lovduv
    Member
    Posted 3 years ago #

    I am trying to do this as well...I will post if I find anything.

    Here is what I have currently and what I want to do...

    Using a "featured" slideshow on home page that pulls images and captions from my "featured" category and displays both in the slideshow. I would like to use external as well as internal links i.e if I have a post that corresponds with the image than it will link to the post...if there is no content then it will use the link url in settings....barring an easier solution...I see it working by using an if statement to check for the existence of content then if content exists use post url else if none use the link url set in the image properties...trying to find out now how wordpress calls the set link for an insert post maybe that will shed some light

    This post discusses http://www.doc4design.com/articles/if-content-exists/ checking if the_content exists for theme structure...I think a modified version of this *could* work...seeing what I can come up with. :)

  4. lovduv
    Member
    Posted 3 years ago #

    Ok definitely think this is possible using custom fields...set the field with the url, then use the if/else statement for link...working on it!

  5. lovduv
    Member
    Posted 3 years ago #

    Ok I did it :)

    So here is what I did:

    I added a custom field called "featured_external" for name and then I put the url I want to link to in for value.

    Then I added this chunk of code where I want my featured images to show...This works because I test for the existence of content in the post...so for it to work you must set the featured image and have NO content...one word in the post and your image will link to the post...There may be a better more elegant way to do this, but it worked for me...

    <?php query_posts( 'cat=Featured&posts_per_page=5' ); if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    <?php $key="featured_external"; $custom = get_post_meta($post->ID, $key, true); ?>
    
    	<?php if(has_post_thumbnail()) {
    			if($content = $post->post_content ) { ?>
    				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<?php the_post_thumbnail(); ?>
    				</a>
    					<?php } else { ?>
    					<a href="<?php $key="featured_external"; echo get_post_meta($post->ID, $key, true); ?>" title="<?php the_title(); ?>">
    					<?php the_post_thumbnail(); ?>
    					</a>
    					<?php }  ?>
    	<?php }  ?>
    	<?php endwhile; endif;?>

    Now this works for MY theme which has a slideshow with the most recent 5 posts, you will need to adapt for yours but this example should get you there : )

  6. ceopowertools
    Member
    Posted 3 years ago #

    Okay lovduv,

    Are you interested in hiring out to do it for me? :) I need this as well but am a real novice when it comes to code stuff. I wonder why nobody has built a plugin for this yet?!?! Maybe that isn't an option and I'm just showing how naive I am. :)

  7. lovduv
    Member
    Posted 3 years ago #

    Hi,

    Sure, but depending on the complexity of what you need (i.e if it's easy), I may just do it for free, cuz I am awesome like that...lol...

    Send me an email with your url and what exactly you are trying to accomplish and we will go from there... support at achievementwebsolutions dot com

  8. lovduv
    Member
    Posted 3 years ago #

    I worked out ceopowertools request...he needed something a bit different, but what I did discover in working on his...all you really need to accomplish an external link on a featured image is two of the above lines from there you can be as complex or as simple as you want. : )

    This will pull your link from the custom field "featured_external":
    <?php $key="featured_external"; $custom = get_post_meta($post->ID, $key, true); ?>

    This will create the link when wrapped around wherever you call the featured image:

    <a>ID, $key, true); ?>" title="<?php the_title(); ?>">
    <?php the_post_thumbnail(); ?>
    </a>
  9. ceopowertools
    Member
    Posted 3 years ago #

    WOW!!! I can't say enough thank-you's to lovdov! She made short work of the problem that had been driving me (and my wife) crazy for a couple of weeks! I HIGHLY recommend her to anyone building something with WordPress! She even took the time and effort to send me some security tips for my site that weren't part of what she was working on. I will definitely be hiring her in the future. Thanks a million for my life MUCH better!

    CEO Powertools

  10. lovduv
    Member
    Posted 3 years ago #

    Thank you kind sir! ; )

  11. stevestavstav
    Member
    Posted 3 years ago #

    Hey, I'm having the same issue and this seems to be the most informative solution I've come across. I've been at this for weeks! Basically, what I'm trying to do is get the featured images on my homepage to link to another url besides the post itself. I hope to set a youtube link as that url so the featured image opens in lightbox as a video within the page. My first stab at making that happen is actually being able to set the featured image to link somehwere other than the full post. creating a custom field seems like the way to go. Lovduv, where would I place those codes? index.php?

  12. pacuthbert
    Member
    Posted 3 years ago #

    Hi Lovduv,
    This is probably a delayed one as well and much appreciated for the solution!

    I think I get it, but I was wondering where in the code would you put the custom link where you actually force the link to the image? I hope that made some sense!

    Thank you again!

    Cheers,
    Ashley

  13. the1path
    Member
    Posted 3 years ago #

    Hi all this was almost what I needed but because I was working with a custom theme the "Featured Image" was not attached to a post but to a custom built "Link Widget". Basically I lacked the ability to create a custom field for the url. So I found another solution which others might adapt, learn from or indeed find easier!

    What I did was put the url I wanted in the description area when you add a Featured Image. Just pasted it straight in there! Then I ...

    Setup the arguments to use with the current post using the current post id (i used a custom variable $number) and put in an array
    $at_args = array( 'post_type' => 'attachment', 'post_parent' => $number );
    /* please not Ive used $number but you may want to use $post->ID as this was a custom function I was using */

    Use the array of arguments use the get_posts() function to get attachment ID data
    $attachment_data = get_posts( $at_args );

    You cant use the ID by default with get_posts() so we must call setup_postdata() and loop through each array item

    foreach( $attachment_data as $post ) : setup_postdata($post);
    endforeach;

    Stick the attachment ID in a variable
    $attachment_id = $attachment_data->ID;

    Use the get_post function to call in an array of post data with the corresponding ID (attachment)
    $link = get_post( $attachment_id );

    Then finally to display the damn url
    <a href="<?php echo $link->post_content; ?>">whatever</a>

    Hmmm once I right it down it doesnt look easier but may help someone!

  14. DHebe
    Member
    Posted 3 years ago #

    the easiest way I found is to go to media where the picture is stored
    or edit image. Wipe out the link url and put in your own, click update.
    You can also just put the url of your choice in as son as up upload the image.

  15. IAMTHELAB
    Member
    Posted 3 years ago #

    lovduv or to any of you talented folks:

    Where would I put the code you suggest? I get the part where you add a custom field to the post but not where to put the code to make it work. Any suggestions?

  16. resonant705
    Member
    Posted 3 years ago #

    having a little bit of trouble with having the title permalink link to the featured external link. The thumbnail works nicely but can't get the text to match the featured external url
    below is the code I used `<!--section_apparel-->
    <div class="section">
    <div class="site_title" id="apparel_site_title"><a name="APPAREL"><img src="<?php bloginfo ('template_directory'); ?>/images/title_apparel.png"/></a> </div><!--site_title-->
    <div id="apparel"><?php query_posts('cat=6&showposts=-1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $key="featured_external"; $custom = get_post_meta($post->ID, $key, true); ?>
    <div class="latest-post-wrap">
    <div class="latest-post">
    <div class="latest-title">
    <h2>

    </h2>
    </div>

    <div class="feat-thumb">

    <?php if(has_post_thumbnail()) {
    if($content = $post->post_content ) { ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php the_post_thumbnail('feat-loop2'); ?>
    </a>
    <?php } else { ?>
    <a href="<?php $key="featured_external"; echo get_post_meta($post->ID, $key, true); ?>" title="<?php the_title(); ?>">
    <?php the_post_thumbnail(); ?>
    </a>
    <?php } ?>
    <?php } ?>
    </div>
    </div>

    </div>
    <?php endwhile;?>
    <!-- END --></div><!--apparel-->
    </div><!--section_apparel-->`

    cheers
    nate

  17. the1path
    Member
    Posted 3 years ago #

    @DHebe - Im sure that doesnt work.

    @Faberuna - the code goes in your page/post template.

    @resonant705 - Why are you using the_permalink() if you are wanting to link to the featured external link?

    Shouldnt your code read:

    <?php if(has_post_thumbnail()) {
     if($content = $post->post_content ) { ?>
      <a href="<?php $key="featured_external"; echo get_post_meta($post->ID, $key, true); ?>" title="<?php the_title(); ?>">
     <?php the_post_thumbnail('feat-loop2'); ?>
     </a>
     <?php } else { ?>
     <a href="<?php $key="featured_external"; echo get_post_meta($post->ID, $key, true); ?>" title="<?php the_title(); ?>">
     <?php the_post_thumbnail(); ?>
     </a>
     <?php } ?>
     <?php } ?>

    Is that what you mean as Im not quite sure I get your issue?

  18. resonant705
    Member
    Posted 3 years ago #

    Thanks for replying!
    I have a section that calls a certain cat to the page.
    I want to have the option of putting a external link on the featured thumbnail (and title) if there is no external link in the custom field then it uses the reg. post or page link.

  19. the1path
    Member
    Posted 3 years ago #

    That isnt the logic you are currently using though.

    Whats this bit doing, is there other code you havent posted:
    if($content = $post->post_content ) { ?>

    Currently your code is saying:

    If post has a thumbnail display the thumbnail with a link to posts permalink

    else display a thumbnail with an external link.

    You ar enot using an argument to find it there is an external url or not, also you are not displaying any clickable link titles/text.

    So you are using the $custom variable to hold the external url using:
    $key="featured_external"; $custom = get_post_meta($post->ID, $key, true);

    You should use this for you if statement:

    if($custom != ""){
    <a href="<?php echo $custom; ?>" title="<?php the_title(); ?>">
     <?php the_post_thumbnail(); ?>
    </a>
    } else {
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
     <?php the_post_thumbnail(); ?>
    </a>
    }
  20. IAMTHELAB
    Member
    Posted 3 years ago #

    Can you do me a favor and simplify all this code-speak?
    a) I need to add something to a custom field. What code goes in the custom field.
    b) Now I need to add some code to the template itself. Where do I put it and what exactly am I putting?

Topic Closed

This topic has been closed to new replies.

About this Topic