WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Listing Posts of a Category on a Post's "Single" Page View (4 posts)

  1. Copernicus
    Member
    Posted 6 years ago #

    I've been trying to figure out a way to list posts (within specific categories) in a sidebar on individual (single) post views.

    Basically, when a post is viewed through "single.php", I'd like to be able to have a sidebar that lists other posts in that same category. I already have conditional tags running in the sidebar, but using "in_category()" doesn't work (without returning a "Missing 1 Argument" error).

    Here's an example:
    The categories might be:

    <ul>
    <li>Pets
    <ul>
    <li>Cats</li>
    <li>Dogs</li>
    <li>Birds</li>
    </ul>
    </li>
    </ul>

    If someone selects "Dogs" they'll, of course, get all the posts in the "Dogs" category and it's easy to have all the posts of the "Dogs" category display in the sidebar of that "Dogs" section (using the "is_category" tag). But, if the visitor views an individual posts through its permalink, I can't figure out a way to get the sidebar to still display the list of posts within the "Dogs" category.

    I hope this makes sense. I've searched & searched. No luck thus far.

  2. moshu
    Member
    Posted 6 years ago #

    Take a look at the in_category on that Conditional_Tags page.

  3. Copernicus
    Member
    Posted 6 years ago #

    Hi moshu, thanks for the help! I have tried in_category -- strangely, it almost works (displaying the posts in the category as desired but ALSO provides me with the error Warning: Missing argument 1 for in_category() in _____/category-template.php on line 170)

    Even though I have the conditional tag of <?php } elseif (in_category('7') && is_single()) { ?> (where "7" is the category of posts I wish to display)

    I don't know how to get beyond that error with the use of in_category within the sidebar.

  4. Copernicus
    Member
    Posted 6 years ago #

    I'm not sure that this is an ideal solution for the issue above, but for anyone else experiencing this same issue, I've found that the "Missing argument 1 for in_category() in _____/category-template.php" error arose from the set-up of the conditional tags within the sidebar.

    Originally, I had employed the following code:

    <div class="sidebar">
    <?php if ( is_404() || is_category() || in_category() || is_single() || is_day() || is_month() || is_year() || is_search() || is_paged() || is_page() ) { ?>
    ...

    This code enabled any of those conditions within the sidebar (the code goes on to lists specific content for any of the individual conditions, such as

    <?php /* } elseif (is_category('1')) { ?>
    <h3>Headline/Content Here</h3>

    etc.)

    Although the other conditional tags were operating correctly with the empty arguments (such as "is_category()"), the empty "in_category()" in that opening
    code above was producing the "Missing argument" error.

    When I changed the code above to

    <div class="sidebar">
    <?php if ( is_404() || is_category() || in_category(7) || is_single() || is_day() || is_month() || is_year() || is_search() || is_paged() || is_page() ) { ?>

    everything started working correctly (with no errors!).

    I assume one would have to add an "in_category" instance for each category that requires a specific conditional tag later in the sidebar's code.

    For example,

    <div class="sidebar">
    <?php if ( is_404() || is_category() || in_category(7) || in_category(8) || in_category(9) || is_single() || is_day() || is_month() || is_year() || is_search() || is_paged() || is_page() ) { ?>

    (where category ID numbers 7,8 & 9 will have specific conditional tags within the sidebar)

    I hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Topic