[resolved] Pulling Current Category (3 posts)

  1. isserdude
    Posted 4 years ago #


    I am trying to have my single posts pull the current category (and subcategory when appropriate) in the sidebar, but I don't know what to replace the "8" with in the following code. When I include a category number (like 8), it works great, but I can't figure out how to automatically have it pull from the current category. Any help would be GREATLY appreciated. I am not a PHP programmer and this is my first time implementing this. Everything else on the site works great, but now I'm stuck with this final piece.

    <?php if ($studiobox_sidebarfeatured_style=="Random from Category") {
    $my_query = new WP_Query('cat=8&showposts=1&orderby=rand');
    } else { $my_query = new WP_Query('cat=8&showposts=1'); } ?>


  2. esmi
    Forum Moderator
    Posted 4 years ago #


    $category = get_the_category();
    $current_category_id =  $category[0]->cat_ID;
    if ($studiobox_sidebarfeatured_style=="Random from Category") {
    $my_query = new WP_Query('cat=' . $current_category_id . '&posts_per_page=1&orderby=rand');
    } else { $my_query = new WP_Query('cat=' . $current_category_id . '&posts_per_page=1'); } ?>
  3. isserdude
    Posted 4 years ago #

    That did it! Thanks so much!

Topic Closed

This topic has been closed to new replies.

About this Topic