• Greetings all,

    I am trying to obtain the category ID(s) of a post currently being viewed (ie, in the single.php). The following code outputs the list of categories on screen (taken from the codex). How it be modified such that instead of echoing results to the screen, be saved as a variable (string). Any ideas?


    foreach((get_the_category()) as $cat) {
    echo $cat->category_id . ',';
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • If it’s just a string of the category IDs separated by a space, then:

    foreach((get_the_category()) as $cat) {
    $my_cats .= $cat->category_id . ' ';
    }

    Note the use of the dot-equals (.= ) method to append the value onto the variable $my_cats. Of course that can be any variable name you choose.

    Thread Starter clarke1866

    (@clarke1866)

    Awesome man! Here is how I implemented it in case someone stumbles upon this in the future. When placed in the single.php, it uses the ‘get recent posts’ plugin by cofee2code and displays only other posts in the same category(ies). Here it is:


    foreach((get_the_category()) as $cat) {
    $max_cat_list .= $cat->category_id . ',';
    }
    c2c_get_recent_posts ($num_posts = 5,
    $format = "

    • %post_URL%
    • ",
      $categories = $max_cat_list,
      $orderby = 'date',
      $order = 'DESC',
      $offset = 0,
      $date_format = 'm/d/Y',
      $authors = '',
      $include_passworded_posts = false)

    Where do I paste the above code in the page.php code?

    ‘<?php get_header(); ?>
    <?php if (!(” == category_description())) : ?>
    <div class=”categorydescription”><?php echo category_description(); ?></div>
    <?php endif; ?>
    <div id=”subsection”>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php link_pages(‘Pages: ‘, ”, ‘number’); ?>

    <?php endwhile; endif; ?>
    </div>
    <?php get_footer(); ?>’

    Thread Starter clarke1866

    (@clarke1866)

    Your setup is different than mine, in my template, I have a single.php page that takes care of viewing a single post. In it, there is a call to get the comments.php, which loads up all the comments and stuff. I have placed my code after the comments include, so that the recent posts show up after the comments. You must have the coffee2code plugin installed and activated. It is here

    I have a single.php which sounds similar to yours. What I’m trying to do is set up a category page, which lists all the posts in a single category at the side bar. It sounds like the code you’ve listed would do that. But I’d want the list in the side bar. Could I use it there? If so, where?

    I have a most recent posts plugin, which may be similar to yours. Here is what I have for the side bar, and where it says “fresh picks” I’d like to list the posts in the category.

    ‘<!– begin catsidebar –>
    <div id=”catsidebar”>

    <?php if (is_single()) { ?>
    <!– HTML OUTPUT FOR IS_SINGLE –>
    <span><?php _e(‘About Entry’); ?></span>
    This entry was posted on <?php the_time(‘l, F jS, Y’); ?> at <?php the_time(); ?> and is filed under <?php the_category(‘, ‘); ?>. You can follow any responses to this entry through the “>RSS 2.0 feed. You can leave a response, or “>trackback from your own site. <?php edit_post_link(‘

    [ Edit this entry ]’,”,”); ?>
    <!– CLOSE OUTPUT –>
    <?php } else { ?>

    <span><?php _e(‘Fresh Picks’); ?></span>

      <?php mdv_recent_posts(); ?>

    <span><?php _e(‘Categories’); ?></span>

      <?php wp_list_cats(); ?>

    <?php } ?>
    </div>
    <!– end catsidebar –>’

    Thread Starter clarke1866

    (@clarke1866)

    I don’t know what you are doing, so I can only tell you what I am doing in more detail in hopes that it helps you. Here is my code again, with comments:


    foreach((get_the_category()) as $cat) {
    $max_cat_list .= $cat->category_id . ',';
    }

    The english translation of the above would be: for the current post, record all the categories it belongs to in the variable max_cat_list.

    Then we use the coffe to code get recent posts plugin to select 5 recent posts. Notice “$categories = $max_cat_list,” meaning, only get the recent posts from the list identified from max_cat_list. Here is the code I used for that specific plugin (not that this code doesn’t do any work, it just calls a function that does all the work):

    c2c_get_recent_posts ($num_posts = 5,
    # $format = ” %post_URL%
    “,
    $categories = $max_cat_list,
    $orderby = ‘date’,
    $order = ‘DESC’,
    $offset = 0,
    $date_format = ‘m/d/Y’,
    $authors = ”,
    $include_passworded_posts = false).

    I hope that helps! I suggest you try the coffee2 code plugin

    Thread Starter clarke1866

    (@clarke1866)

    Maybe you should look at this post?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using get_the_category’ is closed to new replies.