0

I am trying to show twitter product cards. I have this function in my functions.php . I validated the twitter card and everything looks good except the product data. I am trying to display it for only the post type shops.

function add_twitter_cards() {
is_singular( 'shops' ); {
$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];
?>
<meta name="twitter:card" value="product" />
<meta name="twitter:site" value="@mytwitter" />
<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 $twitter; ?>" />
<meta name="twitter:label1" value="Total Sales" />
<meta name="twitter:data1" value="<?php echo $totalsales ?> <?php _e('of', 'sales'); ?> <?php echo $totalsales; ?>" />
<meta name="twitter:label2" value="Amount Earned" />
<meta name="twitter:data2" value="<?php echo countAmountForAll($post->ID); ?>" />


<?
}
}
}
1

You are missing a few parts here to make this work

  • You need set the global $post in your function

  • You cannot use template tags directly without setting up postdata

  • You need to add the post ID to get_permalink()

  • You have a lot of syntax errors. Compare my code with yours

You can try the following: (CAVEAT: Untested)

function add_twitter_cards() 
{
global $post;
    if(is_singular( 'shops' )) {

        setup_postdata($post);
        $tc_url = get_permalink($post->ID);
        $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];
        ?>
        <meta name="twitter:card" value="product" />
        <meta name="twitter:site" value="@mytwitter" />
        <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 $twitter; ?>" />
            <meta name="twitter:label1" value="Total Sales" />
            <meta name="twitter:data1" value="<?php echo $totalsales; ?> <?php _e('of', 'sales'); ?> <?php echo $totalsales; ?>" />
            <meta name="twitter:label2" value="Amount Earned" />
            <meta name="twitter:data2" value="<?php echo countAmountForAll($post->ID); ?>" />

        <?
        }
        wp_reset_postdata();
    }
}
  • The twitter card validator says card error. Thank you though as this does get me closer it seems. – JediTricks007 Feb 12 '15 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.