WordPress.org

Support

Support » Plugins and Hacks » Menu of posts in single.php

Menu of posts in single.php

  • I’m using the following query to generate a menu of posts in my single.php. I need to change the “category-name” part so that it displays the posts in the current category, NOT the posts in the specific, named category.

    <?php
    // this query creates the menu of posts in this category
    $my_query = new WP_Query('category_name=women&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();
    ?>

    At present, I am getting a very nice list of posts, but they are in the “women” category, not the category in which the current post resides. Any help would be greatly appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you know code, this should help: Template_Tags/get_the_category

    moshu to the rescue again! 🙂

    I’m a little unsure how to implement it — how to integrate it with my existing query. I’ve tried a few things…broke it.

    I just want to replace “women” (in the code above) with the output of get_the_category

    Can you please show me how it should be done?

    consider replacing your query with a standards-based approach using wordpress functions. That way, you don’t need to fix your query every time there’s a change to the back-end.

    look at the get_posts function.

    Ok, I’m still working on this to no avail. I really appreciate the suggestions so far, but I guess my coding skills aren’t up to the level needed for this. Can someone give me a very specific code to use?

    My basic goal is simply to display a menu of all the posts in the same category as the current post. I have code which sort of accomplishes this, but it has to be hard-coded with the category name. I would like it to be variable, based on the category of the current post. This seems like a relatively simple thing to do, but I can’t find a plugin or any other instructions to accomplish it. I have tried the above suggestions, but can’t seem to get the code dialed in so that it doesn’t break everything.

    Where it says CATNAME in the code below, I want that to reflect the current category that the post (displayed by single.php) is in.

    <?php
    // this query creates the menu of posts in this category
    $my_query = new WP_Query('category_name=CATNAME&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();
    ?>

    I would be very grateful if someone could show me exactly what to replace CATNAME with, or give me a clear alternative. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Menu of posts in single.php’ is closed to new replies.
Skip to toolbar