Conditionally write open graph meta property in header
-
Have a function I use to print Open Graph tags in the header of each page. I also use a plugin to post to Facebook which also puts in it’s own set of basic OG Tags. Facebook doesn’t like if a page as more than one og:url tag. The plugin only works on single pages so I want to not print my og:url tag when it is on a single post page. I want to test for is_home and is_tag and if those are true the function will print the og:url tag. The other problem is the home page has a completely different url than the tag page. I tried doing the conditional inside the meta property tag but Facebook vomits if the og:url line even exists.
Here is the code I currently have and everything except the conditional for the og:url tag is working:
function wpc_fb_opengraph() {
$wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
?>
<?php echo "\n<!-- Start of OG Tags from DougTheme 2013 -->\n"; ?>
<meta property="og:image" content="<?php if (function_exists('catch_that_image')) {echo catch_that_image(); }?>" />
<meta property="fb:admins" content="XXXXX"/>
<meta property="fb:app_id" content="XXXXXXXXXX">
<meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
<meta property="og:description" content="<?php if ( is_singular() ) { echo strip_tags(get_the_excerpt()); } else { echo "comments about everything"; } ?>" />
<meta property="og:title" content="<?php if ( is_singular()) { echo esc_attr( get_the_title() ); } elseif (function_exists('is_tag') && is_tag()) {single_tag_title('Tag Archive for "'); echo '" - '; echo get_bloginfo('name');} else { echo get_bloginfo('name'); } ?>">
<meta property="og:type" content="<?php if ( is_singular() ) { echo "article"; } else { echo "website";} ?>">
if (is_home()) {
<meta property="og:url" content="<?php bloginfo('url'); ?>" />
<?php } //end if homeelseif ( is_tag() ) {
<meta property="og:url" content="<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>">
<?php }<?php echo "\n<!-- End of OG Tags from DougTheme 2013 -->\n"; ?>
<?php }add_action('wp_head', 'wpc_fb_opengraph');
How do I include the test inside the function?
- The topic ‘Conditionally write open graph meta property in header’ is closed to new replies.