Facebook喜欢Button,Open Graph,Meta Tags和Wordpress中的多个帖子

时间:2011-05-09 16:55:21

标签: facebook wordpress facebook-like meta opengraph

我有一个wordpress网站,其中包含为每个帖子生成的类似按钮,并以the_permalink作为网址。我已经在模板的标题中设置了标准的opengraph标签,但当然问题是每个单独的按钮都会发布相同的标题,描述和图像等。

问题在于我无法在循环中设置标记,因为元标记必须位于标题中?

这个问题有解决办法吗?我尝试了很多插件,但它们看起来都过于复杂,很难在模板中正确定位。

1 个答案:

答案 0 :(得分:1)

我想你可以做以下其中一项:
1-手动将元标记放置到标题中:

<meta property="fb:admins" content="XXXXXXX"/>
<meta property="og:site_name" content="Example.com"/>
<meta property="og:image" content="http://www.example.com/image.png"/>
<?php if (is_front_page()) : ?>
<meta property="og:type" content="blog"/>
<meta property="og:description" content="test test test test"/>
<meta property="og:title" content="My title"/>
<meta property="og:url" content="<?php echo get_bloginfo('home'); ?>"/>
<?php elseif (is_single() || is_page()) : ?>
<meta property="og:type" content="article"/>
<meta property="og:title" content="<?php echo trim(wp_title('', false)); ?>"/>
<meta property="og:url" content="<?php echo get_permalink(); ?>"/>
<?php elseif (!is_front_page() && !is_single() && !is_page()) : ?>
<meta property="og:title" content="<?php echo trim(wp_title('', false)); ?>"/>
<?php endif ?>

或者如果你想使用hooks(functions.php):

add_action('wp_head', 'add_og_meta_tags');
function add_og_meta_tags() {
echo '<meta property="fb:admins" content="XXXXXXX"/>
<meta property="og:site_name" content="Example.com"/>
<meta property="og:image" content="http://www.example.com/image.png"/>';
if (is_front_page()) :
echo '<meta property="og:type" content="blog"/>
<meta property="og:description" content="test test test test"/>
<meta property="og:title" content="My title"/>
<meta property="og:url" content=" '. get_bloginfo('home') . '"/>';
elseif (is_single() || is_page()) :
echo '<meta property="og:type" content="article"/>
<meta property="og:title" content="' . trim(wp_title('', false)) . '"/>
<meta property="og:url" content="' . get_permalink() .'"/>';
elseif (!is_front_page() && !is_single() && !is_page()) :
echo '<meta property="og:title" content="' . trim(wp_title('', false)) .'"/>';
endif;
}
相关问题