Support » Fixing WordPress » Pulling Current Category

  • Resolved isserdude

    (@isserdude)


    Hi,

    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’); } ?>

    Thanks!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • esmi

    (@esmi)

    Forum Moderator

    Try:

    <?php
    $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'); } ?>

    That did it! Thanks so much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Pulling Current Category’ is closed to new replies.