WordPress.org

Ready to get started?Download WordPress

Forums

Adding custom image to Facebook (10 posts)

  1. baal666
    Member
    Posted 4 years ago #

    Hi,

    I'd like to add a custom image to the Facebook link. Right now each image on each of my post is too big for Facebook to consider it. I'd like to find a way to make a smaller version that would show on Facebook link. I can do one for the who blog that would be linked from the header, but I'd like that image to change depending on the post.

    Any idea?

    Thanks

  2. baal666
    Member
    Posted 4 years ago #

    Anyone?

  3. baal666
    Member
    Posted 4 years ago #

    Maybe my question is not clear enough.

    Ok, let me reformulate.

    I already show wide images on my blog, but they are too big to be taken care of by facebook. I am looking for a way to integrate images to Facebook links for each of my posts. Any idea?

  4. baal666
    Member
    Posted 4 years ago #

    My image is 650 px width... is it why Facebook does not take it? How can I do anything to go around this?

    Facebook documentation

    says that this in the header would work:

    <link rel="image_src" href="http://www.onjd.com/design05/images/PH2/WableAFC205.jpg" />

    But how to make that link change each time there is a new post and link to another smaller version of the bigger image?

  5. Tim Nicholson
    Member
    Posted 4 years ago #

    bal, try using the WP built-in function to get the thumbnail version of the image. You can set the size of the thumbnail that WP will create when you upload the image on your post.

    The code should look something like this:

    //Get images attached to the post
    $args = array(
    	'post_type'      => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts'    => -1,
            'order'          => 'ASC',
    	'post_status'    => null,
    	'post_parent'    => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		$img = wp_get_attachment_thumb_url( $attachment->ID );
    echo '<link rel="image_src" href="'.$img.'">';
                    break;
            }

    Also check out this function, which could be used to pull the "medium" or "large" version of the uploaded image:
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

  6. baal666
    Member
    Posted 4 years ago #

    The problem is that I don't want the thumbnail to get displayed; I want regular visitors to see my 650 width pix... Smaller image is only for Facebook.

  7. agrestik
    Member
    Posted 4 years ago #

    This is exactly what I was looking for! Thanks so much timnicholson!

  8. Tim Nicholson
    Member
    Posted 4 years ago #

    baal666, did you not actually try what I posted? It doesn't display a thumbnail in your post, it adds a <link rel="image_src" href="image-thumbnail"> line of code, which as you pointed out from the Facebook docs, tells Facebook what image(s) to show when you share/link to a post. This works on my site at http://xtremelysocial.com. Note that it only works when you use the wordpress image uploader. If you hard code a link to an image in the post it doesn't work.

  9. aricberg
    Member
    Posted 4 years ago #

    timnicholson, Where exactly would I paste that code? I just yesterday started having this problem and am not sure why! Thank you for your help!

  10. lerry
    Member
    Posted 4 years ago #

    Thanks for all, helpful information here.

Topic Closed

This topic has been closed to new replies.

About this Topic