Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Open Graph meta information for Facebook and Google+
<!-- Open graph meta -->
<meta property="og:type" content="website">
<meta property="og:site_name" content="<?php echo site_name(); ?>">
<?php if(is_homepage() == true) : ?>
<meta property="og:url" content="http://websiteaddress.com">
<meta property="og:title" content="<?php echo site_name(); ?>">
<meta property="og:description" content="<?php echo site_description(); ?>">
<?php else: ?>
<meta property="og:url" content="<?php echo "http://websiteaddress.com/" . current_url(); ?>">
<meta property="og:title" content="<?php echo page_title('Page can’t be found'); ?> - <?php echo site_name(); ?>">
<meta property="og:description" content="<?php if(strlen(article_description())) : echo article_description(); else: echo site_description(); endif; ?>">
<?php endif; ?>
<!-- If you have any image custom fields -->
<?php $thumbnail = article_custom_field('thumbnail');
if ( !empty($thumbnail) ) : ?>
<meta property="og:image" content="<?php echo "http://websiteaddress.com" . $thumbnail; ?>">
<?php else: ?>
<meta property="og:image" content="<?php echo "http://websiteaddress.com" . theme_url('img/logo.png'); ?>">
<?php endif; ?>

pReya commented Aug 4, 2013

Hey David,

i really appreciate this snippet, even though it doesn't seem to work perfectly. Somehow i always end up with the general site description and title, even though i am using an article url. Just like "chrisod" in the Anchor Forums: http://forums.anchorcms.com/discussion/linking-to-anchor-posts-from-facebook-google

Is there any chance you can help me, integrating the Open Graph Meta data correctly into my Anchor Theme (http://bushwick.molovo.co.uk/). I'd really appreciate it!

Best,
Moritz

Owner

daviddarnes commented Aug 31, 2013

@pReya What kind of error are you getting?

pReya commented Sep 19, 2013

Hey David, sorry for the silence. Your code does not pull the article description, even though i set both arguments to "article_description". It also does not grab my sidebar image, which i use as a custom field (theme is Bushwick by molovo). Here's my code, maybe i'm just too dumb?

<!-- Open graph meta -->
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="<?php echo site_name(); ?>">
<?php if(is_homepage() == true) : ?>
    <meta property="og:url" content="http://netchild.de">
    <meta property="og:title" content="<?php echo site_name(); ?>">
    <meta property="og:description" content="<?php if(strlen(article_description())) : echo article_description(); else: echo site_description(); endif; ?>">
<?php else: ?>
    <meta property="og:url" content="<?php echo "http://netchild.de/" . current_url(); ?>">
    <meta property="og:title" content="<?php echo page_title('Page can’t be found'); ?> - <?php echo site_name(); ?>">
    <meta property="og:description" content="<?php if(strlen(article_description())) : echo article_description(); else: echo site_description(); endif; ?>">
<?php endif; ?>
<!-- If you have any image custom fields -->
<?php $thumbnail = article_custom_field('sidebar_image');
    if ( !empty($sidebar_image) ) : ?>
    <meta property="og:image" content="http://netchild.de/content/sidebar-bg2.jpg">
<?php else: ?>
    <meta property="og:image" content="http://netchild.de/content/sidebar-bg2.jpg">
<?php endif; ?>

cinoyter commented Jan 8, 2014

It seems that is_homepage() == true is always evaluating to true, so it always writes out the meta tags for the homepage, not the posts. As a workaround, I replaced this expression with:

 if(current_url() == "/" || current_url() == "posts") :

and it seems to work correctly.

dannyrb commented Mar 10, 2015

@cinoyter -- I had a similar issue with is_homepage(); Thanks for taking the time to post a fix. I also found this write-up that shows how to add support Twitter Cards and Search Engine MicroData:

http://lug.io/blog/posts/adding-semantic-markup-to-anchor-cms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment