Support » Fixing WordPress » What is the permalink to a post's featured image?

  • Similar to the Featured Link Image plugin, I’d like to get the permalink of a post’s Featured Image. I see that it can be done with php, but this is for a page that has a lot of text and uses hard-coded permalinks to which I would like to add featured images.

    Is it possible to add a Custom field for wp_get_attachment_image_src or something like that?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Look at the examples here:
    http://codex.wordpress.org/Function_Reference/the_post_thumbnail#Examples

    Or try it with something like this (inside the loop):

    <?php
    $post_thumbnail_id = get_post_thumbnail_id( $post->ID);
    if($post_thumbnail_id) {
      the_attachment_link( $post_thumbnail_id );
    }
    ?>

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Thank you, that makes sense, but I’m doing this entirely outside the loop. How would I apply that?

    There’s a page that’s something like:

    This is the story of my travels. Out first car trip place was to <a href="/cheyenne">Cheyenne, Wyoming</a>. Our next trip was <a href="/venezuela">Venezuela</a>. We are looking forward to returning to <a href="/india">India</a> for the tenth time.

    The text is a lot more complicated than that, and much longer, but basically I’d like to call the featured image outside of a loop, so it would be part of this text.

    Is there any way to use the code you have in the body of a page, perhaps with php enabled for posts? Then the above would be something like:

    This is the story of my travels. Out first car trip place was to <a href="<?php
    $post_thumbnail_id = get_post_thumbnail_id( $post->107);
    if($post_thumbnail_id) {
      the_attachment_link( $post_thumbnail_id );
    }
    ?>">Cheyenne, Wyoming</a>. Our next trip was <a href="<?php
    $post_thumbnail_id = get_post_thumbnail_id( $post->112);
    if($post_thumbnail_id) {
      the_attachment_link( $post_thumbnail_id );
    }
    ?>">Venezuela</a>. We are looking forward to returning to <a href="<?php
    $post_thumbnail_id = get_post_thumbnail_id( $post->165);
    if($post_thumbnail_id) {
      the_attachment_link( $post_thumbnail_id );
    }
    ?>">India</a> for the tenth time.

    I’m using a child theme with thematic and can add a function to it, but I can’t seem to get a handle on what this would be.

    Moderator keesiemeijer

    (@keesiemeijer)

    So you need post thumbnails from other posts in the post content? Try it with a shortcode. Put this in your theme’s functions.php:

    function add_post_thumbnail_to_content( $atts ) {
    	extract( shortcode_atts( array(
    		'post_id' => '',
    		'size' => 'thumbnail',
    	), $atts ) );
    
      if($post_id && has_post_thumbnail($post_id)) {
         $image = get_the_post_thumbnail( $post_id, $size);
         $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $image . '</a>';
        return $html; 
    
      }
    
    }
    add_shortcode( 'post_thumbnail', 'add_post_thumbnail_to_content' );

    Now you can use this in your post content to show a post thumbnail from a post with ID 23:

    [post_thumbnail post_id="23"]

    This will output a thumbnail from post ID 23. If you want to show a different size you can set the size parameter as well (thumbnail, medium, large or full)

    [post_thumbnail post_id="23" size="large"]

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Moderator keesiemeijer

    (@keesiemeijer)

    Ah, I see you don’t want to display the image, just link to it. Try it with this in your functions.php:

    function add_post_thumbnail_link( $atts, $content = null ) {
    	extract( shortcode_atts( array(
    		'post_id' => '',
    		'size' => 'full',
    	), $atts ) );
    
      if($post_id && has_post_thumbnail( $post_id )) {
         $thumbnail_id = get_post_thumbnail_id( $post_id );
         $image_src = wp_get_attachment_image_src( $thumbnail_id, $size);
         $html = '<a href="' . $image_src[0] . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $content . '</a>';
        return $html; 
    
      }
    
    }
    add_shortcode( 'post_thumbnail_link', 'add_post_thumbnail_link' );

    Use the shortcode like this

    This is the story of my travels. Out first car trip place was to [post_thumbnail_link post_id="23"]Cheyenne, Wyoming[/post_thumbnail_link]. Our next trip was ....

    keesiemeijer, it’s the first code I was looking for, displaying thumbnails from posts in another post’s content. It works just right. The second shortcode may also prove useful.

    Thank you!!! Kathy

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘What is the permalink to a post's featured image?’ is closed to new replies.