0

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

问题在于我不能在循环中设置标签,因为元标签必须在标题中?

这个问题有什么解决办法吗?我尝试了许多插件,但它们似乎都过于复杂且难以在模板中正确定位。

4

1 回答 1

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 ?>

或者如果你想使用钩子(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;
}
于 2011-05-09T18:22:02.990 回答