waffl
waffl

Reputation: 5511

Facebook Like Button, Open Graph, Meta Tags and multiple posts in Wordpress

I have a wordpress site with like buttons that are generated for each post and given the_permalink as the url. I have set the standard opengraph tags in the header of the template, but then of course the problem is that every single like button will post the same title, description and image etc.

The problem lies in that I can't set the tags within the loop because the meta tags have to be in the header?

Is there any solution to this problem? I've tried a number of plugins but they all seem overly complicated and hard to position properly within the template.

Upvotes: 0

Views: 2238

Answers (1)

ifaour
ifaour

Reputation: 38135

I suppose you could do one of the following:
1- Placing the meta tags to the header manually:

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

Or if you want to use 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;
}

Upvotes: 1

Related Questions