WordPress.org

Ready to get started?Download WordPress

Forums

Menu of posts in single.php (5 posts)

  1. kalico
    Member
    Posted 5 years ago #

    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.

  2. moshu
    Member
    Posted 5 years ago #

    If you know code, this should help: Template_Tags/get_the_category

  3. kalico
    Member
    Posted 5 years ago #

    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?

  4. Ivovic
    Member
    Posted 5 years ago #

    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.

  5. kalico
    Member
    Posted 5 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic