Support » Plugins » Grab Post Category for re-tweet link

  • Resolved cozbaldwin


    Hello all,

    I’m using the simple “Tweet This!” snippet in my loop which grabs the Post Title and the post URL and prints them in the Twitter update box.

    <a target="_blank" href=" it out!%20<?php the_title(); ?>:%20<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>">Tweet This!</a>

    What I WANT to do is also grab the category that post is classified under and print that, too. But I’m a bit ignorant when it comes to this and my attempts at the obvious solutions are all failing. For instance: it out!%20<?php the_category(); ?>%20<?php the_title(); ?>:%20<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>">Tweet This! All this does is break the Tweet This! link on the page.
    (Notice the addition of <?php the_category(); ?>)

    Can anyone provide me with the correct way to call and print the category properly?

Viewing 6 replies - 1 through 6 (of 6 total)
  • you can use this

    foreach((get_the_category()) as $category) {
        $cat_name .= $category->cat_name . ' ';
    echo $cat_name;

    you can read get_the_category for more explanation

    [moderated fixed backtick]

    That kind of works. But there’s a problem…
    Keep in mind I have this in the header of each post.

    When I view my blog’s Archive… lets say February 2010… it brings back all the posts for that month on one page. The first post’s TWEET THIS! link works just like it should, thanks to your help. But as I scroll down to previous posts on the page, the category gets printed multiple times.

    For instance, if I click on the 3rd story down, and if the category is “News”, the Twitter status now prints as “Check it out! News News News MyBlogTitle: http://MyBlogUrl?p=495
    See? “News” gets printed 3 times. The 7th story down would echo News 7 times in the status.

    after you echo the $cat_name then use this to delete the variable

    unset ($cat_name);

    put the code before the cycle of the new loop

    Hm, I might be doing this wrong.
    I now have

    <?php foreach((get_the_category()) as $category) {
        $cat_name .= $category->cat_name . ' ';

    way up at the beginning of the page.

    In the loop I now have:
    <a target="_blank" href=" Blogs' <?php echo $cat_name; ?><?php unset ($cat_name); ?>-%20<?php the_title(); ?>:%20<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>">Tweet This!</a>

    Result, when viewing the February 2010 archive page: The first post works perfect, but all other posts below do not echo the category.

    Ups, may be i explain it in wrong way. all of this code must be inside the loop.

    foreach((get_the_category($post->ID)) as $category) {
        $cat_name .= $category->cat_name . ' ';
        //break;  //if you only need one category per post
    echo $cat_name;
    unset ($cat_name);

    ps : i make some changes.
    sometimes a post has more then one category, the break will help you took one.

    AH! Perfect.
    Thanks for your help!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Grab Post Category for re-tweet link’ is closed to new replies.