Skip to content

Instantly share code, notes, and snippets.

@zoerooney
Last active January 26, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zoerooney/8239842 to your computer and use it in GitHub Desktop.
Save zoerooney/8239842 to your computer and use it in GitHub Desktop.
WordPress Twitter card info (assumes you've already got basic open graph data via a plugin), tutorial here: http://zoerooney.com/blog/tutorials/increase-share-ability-using-twitter-cards/
<?php // Include Twitter IDs for content creator (you, usually) ?>
<meta name="twitter:site" content="@zoe_rooney"/>
<meta name="twitter:creator" content="@zoe_rooney"/>
<?php
// get current post ID
$currentpost = get_the_ID();
// get radio value
$radio = get_field( 'twitter_card_options', $currentpost );
// assign card type to variable based on radio selection
if ( $radio == "Use gallery card type" ) :
$cardtype = 'gallery';
elseif ( $radio == "Use photo card type" ) :
$cardtype = 'photo';
else :
// default to summary large image
$cardtype = 'summary_large_image';
endif;
?>
<?php // meta field for card type with variable in place ?>
<meta name="twitter:card" content="<?php echo $cardtype; ?>"/>
<?php
// add four specific images if using gallery card using image fields
if ( $radio == "Use gallery card type" ) :
// set a bunch of variables for our image data
// I'm using thumbnails to keep the file size down
// thumbnail size must be larger than 160px square
$image0 = get_field( 'gallery_card_image_1', $currentpost );
$image0_url = wp_get_attachment_image_src( $image0, 'thumbnail' );
$image1 = get_field( 'gallery_card_image_2', $currentpost );
$image1_url = wp_get_attachment_image_src( $image1, 'thumbnail' );
$image2 = get_field( 'gallery_card_image_3', $currentpost );
$image2_url = wp_get_attachment_image_src( $image2, 'thumbnail' );
$image3 = get_field( 'gallery_card_image_4', $currentpost );
$image3_url = wp_get_attachment_image_src( $image3, 'thumbnail' );
?>
<meta name="twitter:image0" content="<?php echo $image0_url[0]; ?>"/>
<meta name="twitter:image1" content="<?php echo $image1_url[0]; ?>"/>
<meta name="twitter:image2" content="<?php echo $image2_url[0]; ?>"/>
<meta name="twitter:image3" content="<?php echo $image3_url[0]; ?>"/>
<?php endif; ?>
<?php
// bonus: gallery image code if you're using the ACF gallery field type
if ( $radio == "Use gallery card type" ) :
// Get gallery field data
$twitterimages = get_field( 'twitter_card_gallery', $currentpost );
// set a counter to start at 0 and loop through the images in the gallery
$index = 0;
foreach ( $twitterimages as $image ) : ?>
// display the meta elements with the correct index number and image URLs
<meta name="twitter:image<?php echo $index; ?>" content="<?php echo $image['sizes']['thumbnail']; ?>"/>
<?php $index++; // advance counter
endforeach; // end loop
endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment