WordPress.org

Ready to get started?Download WordPress

Forums

Facebook Social Plugins
og:image is not the post thumbnail (2 posts)

  1. 8.am
    Member
    Posted 2 years ago #

    I've already changed that in the code, but I'd suggest to put that into the plugin:

    socialplugins.php
    Change the whole function AddMetas()

    function AddMetas() {
    		if (is_single() || is_page()) {
    			global $post;
    
    		  echo "<meta property=\"og:site_name\" content=\"".wp_specialchars(get_option('blogname'))."\" />\r\n";
              echo "<meta property=\"og:title\" content=\"".wp_specialchars($post->post_title)."\" />\r\n";
              echo "<meta property=\"og:type\" content=\"article\" />\r\n";
              echo "<meta property=\"og:url\" content=\"".get_permalink()."\" />\r\n";
    
              if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                  $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
                  echo "<meta property=\"og:image\" content=\"".$thumbnail[0]."\" />\r\n";
              } else {
                  $imgs = $this->getImages();
                  if (count($imgs) > 0) {
          	        echo "<meta property=\"og:image\" content=\"".wp_get_attachment_thumb_url($imgs[0]->ID)."\" />\r\n";
                  }
              }
    	  }
    	}

    Now it checks if there is any post thumbnail, it not, it takes the first attachment (as before).

    http://wordpress.org/extend/plugins/facebook-social-plugins/

  2. illabilis
    Member
    Posted 2 years ago #

    Hi
    I want to select wich image apears when i share a post on facebook. But with this code, also apear first image....
    Please help:)
    An exemple post on my blog
    http://gasparfoto.ro/2012/05/anca-cipri-sedinta-inainte-de-nunta.html
    and the modified code:

    function AddMetas() {
    if (is_single() || is_page()) {
    global $post;

    echo "<meta property=\"og:site_name\" content=\"".wp_specialchars(get_option('blogname'))."\" />\r\n";
    echo "<meta property=\"og:title\" content=\"".wp_specialchars($post->post_title)."\" />\r\n";
    echo "<meta property=\"og:type\" content=\"article\" />\r\n";
    echo "<meta property=\"og:url\" content=\"".get_permalink()."\" />\r\n";

    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
    echo "<meta property=\"og:image\" content=\"".$thumbnail[0]."\" />\r\n";
    } else {
    $imgs = $this->getImages();
    if (count($imgs) > 0) {
    echo "<meta property=\"og:image\" content=\"".wp_get_attachment_thumb_url($imgs[0]->ID)."\" />\r\n";
    }
    }
    }
    }

    function getImages() {
    global $post;
    $imgs = get_children('post_type=attachment&post_mime_type=image&post_parent='.$post->ID);
    if (is_array($imgs)) {
    return array_values($imgs);
    } else {
    return array();
    }
    }

    function LikeButton($content) {
    $options = get_option('olussier-facebook-social-plugins');.............................etc

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic