Last active
June 23, 2016 11:03
-
-
Save nmyers/d71427aa2a046f4bfbb6 to your computer and use it in GitHub Desktop.
Facebook open graph and Twitter card
add in functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_opengraph_markup() { | |
if (is_single()) { | |
global $post; | |
if(get_the_post_thumbnail($post->ID, 'thumbnail')) { | |
$thumbnail_id = get_post_thumbnail_id($post->ID); | |
$thumbnail_object = get_post($thumbnail_id); | |
$image = $thumbnail_object->guid; | |
} else { | |
// set default image | |
$image = ''; // | |
} | |
//$description = get_bloginfo('description'); | |
$description = substr(strip_tags($post->post_content),0,200) . '...'; | |
?> | |
<meta property="og:title" content="<?php the_title(); ?>" /> | |
<meta property="og:type" content="article" /> | |
<meta property="og:image" content="<?=$image?>" /> | |
<meta property="og:url" content="<?php the_permalink(); ?>" /> | |
<meta property="og:description" content="<?=$description?>" /> | |
<meta property="og:site_name" content="<?=get_bloginfo('name')?>" /> | |
<?php | |
} | |
} | |
add_action('wp_head', 'add_opengraph_markup'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_opengraph_markup() { | |
if (is_single()) { | |
global $post; | |
$image = get_image(get_field('main_image'),'692x410'); | |
$description = substr(strip_tags($post->post_content),0,200) . '...'; | |
?> | |
<meta property="og:title" content="<?php the_title(); ?>" /> | |
<meta property="og:type" content="article" /> | |
<meta property="og:image:url" content="<?=$image?>" /> | |
<meta property="og:url" content="<?php the_permalink(); ?>" /> | |
<meta property="og:description" content="<?=$description?>" /> | |
<meta property="og:site_name" content="<?=get_bloginfo('name')?>" /> | |
<meta name="twitter:card" content="summary_large_image"> | |
<meta name="twitter:site" content="@fathomjournal"> | |
<meta name="twitter:title" content="<?php the_title(); ?>"> | |
<meta name="twitter:description" content="<?=$description?>"> | |
<meta name="twitter:image" content="<?=$image?>"> | |
<?php | |
} | |
} | |
add_action('wp_head', 'add_opengraph_markup'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment