Created
September 2, 2017 18:55
-
-
Save topleague/5d054f38d2ddfff897dd23da2ab9379b to your computer and use it in GitHub Desktop.
Code for Adding Twitter Cards
This file contains 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
//Code for Adding Twitter Cards | |
add_action('wp_head', 'add_twitter_cards'); | |
function add_twitter_cards() { | |
if(is_single()) { | |
$tc_url = get_permalink(); | |
$tc_title = get_the_title(); | |
$tc_description = get_the_excerpt(); | |
$tc_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), full ); | |
$tc_image_thumb = $tc_image[0]; | |
$tc_author = str_replace('@', '', get_the_author_meta('twitter')); | |
?> | |
<meta name="twitter:card" value="summary_large_image" /> | |
<meta name="twitter:site" value="@league_wp" /> | |
<meta name="twitter:title" value="<?php echo $tc_title; ?>" /> | |
<meta name="twitter:description" value="<?php echo $tc_description; ?>" /> | |
<meta name="twitter:url" value="<?php echo $tc_url; ?>" /> | |
<?php if($tc_image) { ?> | |
<meta name="twitter:image" value="<?php echo $tc_image_thumb; ?>" /> | |
<?php } if($tc_author) { ?> | |
<meta name="twitter:creator" value="@<?php echo $tc_author; ?>" /> | |
<? | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment