Last active
December 11, 2015 14:48
-
-
Save vilmosioo/4616722 to your computer and use it in GitHub Desktop.
Adding Twitter cards to a WordPress website. Add this to your header.php
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
<?php | |
if(is_singular()) { ?> | |
<meta name="twitter:card" content="summary"> | |
<meta name="twitter:url" content="<?php echo get_permalink($post->ID); ?>"> | |
<meta name="twitter:title" content="<?php echo $post->post_title;?>"> | |
<meta name="twitter:description" content="<?php echo substr( $post->post_content, 0, 150 );?>"> | |
<?php | |
// you will need to hardcode this or use a plugin that allows twitter info to be added to authors | |
$twitter = the_author_meta('twitter', $post->post_author); | |
if($twitter){ | |
echo "<meta name='twitter:creator' content='$twitter'>"; | |
} | |
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); | |
if($url){ | |
echo "<meta name='twitter:image' content='$url'>"; | |
} | |
?> | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment