WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Grab Post Category for re-tweet link (7 posts)

  1. cozbaldwin
    Member
    Posted 4 years ago #

    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="http://twitter.com/home?status=Check 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: http://twitter.com/home?status=Check 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?

  2. uwiuw
    Member
    Posted 4 years ago #

    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]

  3. cozbaldwin
    Member
    Posted 4 years ago #

    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.

  4. uwiuw
    Member
    Posted 4 years ago #

    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

  5. cozbaldwin
    Member
    Posted 4 years ago #

    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="http://twitter.com/home?status=My 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.

  6. uwiuw
    Member
    Posted 4 years ago #

    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.

  7. cozbaldwin
    Member
    Posted 4 years ago #

    AH! Perfect.
    Thanks for your help!!!

Topic Closed

This topic has been closed to new replies.

About this Topic