Facebook在Header.php中点赞打开图形元

时间:2012-04-02 作者:redconservatory

我想在标题中添加开放图元标记。php,但有人能解释一下它是如何工作的吗。php带着相关信息出现在我的循环之前??

<meta property="og:title" content="The Rock"/>
<meta property="og:type" content="movie"/>
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:site_name" content="IMDb"/>
<meta property="fb:admins" content="USER_ID"/>
<meta property="og:description"
          content="A group of U.S. Marines, under command of
                   a renegade general, take over Alcatraz and
                   threaten San Francisco Bay with biological
                   weapons."/>
例如:

<meta property="og:description" content="<?php the_content(); >?"/>
无法工作,因为标题。php文件位于循环之前。。。有什么建议吗?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

我有一段时间以前的这个类,它在使用admin_head 行动挂钩

<?php
/**
 * Simple facebook open graph class for WordPress
 * 
 * @author Ohad Raz 
 * @version 0.1
 * 
 */
class simple_open_graph{

    /**
     * Holds the default image url
     * @var string
     * @since 0.1
     */
    public $default_image_url;

    /**
     * holds facebook user id for insights (analytics of the Like Buttons)
     * @var string
     * @since 0.1
     */
    public $facebook_account_id;

    /**
     * Class Constructor
     * 
     * @since 0.1
     * @author Ohad Raz
     * @access public
     * 
     * @return Void
     */
    public function __construct(){
        $this->default_image_url = "http://www.example.com/image.jpg";
        $this->facebook_account_id = "Your Facebook Account ID";

        add_action( \'wp_head\', array($this,\'facebook_open_graph_meta_head\'), 5 );
    }

    /**
     * function to get the content type
     * 
     * @since 0.1
     * @author Ohad Raz
     * @access public
     * 
     * @return (string) either website or article based on the curent page
     */
    public function content_type(){
        if (is_single() || is_page())
            return "article"; 
        else 
            return "website";
    }

    /**
     *  function to get the image from post_thumb, post content or default image
     *  
     *  @since 0.1
     * @author Ohad Raz
     * @access public
     * 
     * @return (string) image src
     */
    public function graph_image(){
        if ((function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())) {
            $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'\', \'\' );
            return $src[0];
        } else {
            global $post, $posts;
            $fbimage = \'\';
            $output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\',
            $post->post_content, $matches);
            return $matches [1] [0];
        }

        return $this->$default_image_url;
    }

    /**
     * Function that actually prints the open graph meta tags
     *   
     * @since 0.1
     * @author Ohad Raz
     * @access public
     * 
     * @return Void
     */
    public function facebook_open_graph_meta_head() {
        global $post;
        echo \'<meta property="fb:admins" content="\'. $facebook_account_id .\'"/>\'; ?>
        <meta property="og:title" content="
        <?php 
            if(is_home()) {
                bloginfo(\'name\'); 
            } elseif(is_category()) { 
                echo single_cat_title();
            } elseif(is_author()) { 
                $curauth = (get_query_var(\'author_name\')) ? get_user_by(\'slug\', get_query_var(\'author_name\')) : get_userdata(get_query_var(\'author\')); 
                echo $curauth->display_name; 
            } else { 
                echo the_title(); 
            } ?>" />
        <meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>"/>
        <meta property="og:url" content="<?php the_permalink(); ?>"/>
        <meta property="og:image" content="<?php echo $this->graph_image(); ?>"/>
        <meta property="og:type" content="<?php echo $this->content_type(); ?>"/>
        <meta property="og:site_name" content="<?php bloginfo(\'name\'); ?>"/>
        <?php
    }

}//end class

结束