Support » Themes and Templates » WP_query to put a post from one category in sidebar?

  • Resolved Kyala


    Hello, everyone!

    I’m tweaking a template in an attempt to put the latest post from a specific category into the sidebar, as kind of a “featured post.” From the codex, I understand that I should use wp_query to do this because it’s happening outside the_loop.

    The code I’m trying to use is

    <?php $videoPosts = new WP_Query('cat=7&showposts=1'); ?>
         <?php while ($videoPosts->have_posts()) : $videoPosts->the_post(); ?>
         <?php the_content(); ?>
      <?php endwhile; ?>

    If I remove the “cat=7” from the first line, it works–it displays the latest post made (not specified by category).

    However, once I put the “cat=7” into the code, it stops working altogether; nothing shows up at all, from any category.

    I’ve been through the codex and the forums, but I can’t figure out where I’m going wrong. I’ve also tried using “category_name” instead of “cat”, replacing the variable with the name of the category. No luck there, either.

    Can a kind guru please tell me what I’m doing wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Code works fine for me in the sidebar.php of the WordPress Default Theme. Wonder if you have a plugin that’s causing the problem?

    Course I used cat=1 and changed the_content to the_title but that shouldn’t matter.

    Doh! I’m an idiot, the plugins should have been the first thing I checked. You’re absolutely right, one of them was interfering with the display.

    Thanks so much for the quick reply!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_query to put a post from one category in sidebar?’ is closed to new replies.