Support » Fixing WordPress » “Single post” combined with “Exclude Categories”

  • Resolved jeff_n


    I’m trying to get my main page (based on the Kubrick theme) to display only a single post. However, I’m also trying to get the main page to never display my posts from the a certain category. I do this by using the code from the codex “The WordPress loop” example:

    <?php if ( !(in_category(‘3’)) ) { ?>

    However, when this code is combined with having a single post on the page, the post comes out blank.

    Normally, with multiple posts on one page, the loop will get a bunch of posts and simply do nothing (skip) when a certain category is reached. However, with a single post on the page, the loop will simply do nothing, leaving an empty post with Previous and Next links.

    Is there any way I can get a single post to display on my main page, while excluding a certain category at the same time?

Viewing 8 replies - 1 through 8 (of 8 total)
  • When you say “single” you mean one (1) post?
    In that theme you are using the so-called ‘single post view’ is handled by the single.php template and not the index.

    Sorry, I do mean one post on the main index, not the single post template. The single post template I believe is used as a permalink, which includes all the comments. However, what I want is the main page to display only one post per page (which I can set with the “Show at most: 1 post” under options), but to exclude a specific category.

    So what you are saying is that the “exclude code” works if more posts are shown on the main page (index) and it doesn’t work if it is set to show only 1 post?
    Showing only one post is always a bit tricky, I remember some similar questions and maybe even some solution was given…

    Yes, that’s it exactly. For example, if you take a look at the code:

    <?php while (have_posts()) : the_post();?>

    <?php if ( !(in_category(‘6’)) ) { ?>

    //…do stuff

    <?php } ?>

    <?php endwhile; ?>

    When there are multiple posts displayed on the page, the loop will only do something if the post is not from category 6 (and consequently skip the post text from being displayed). However, when there is only one post displayed AND it is in category 6, it will simply skip the post text and display my template with no text. I would like it instead to possibly run another query and get the next post.

    By the way, congratulations on your two years at the WordPress forums, Moshu. 🙂

    Heh, thx 🙂

    I don’t really know what to say, why it doesn’t work – I am not a coder by any means.
    You may want to try some other method, like the query_posts for customizing your Loop. Template_Tags/query_posts

    I seem to have fixed the problem, with a plug-in by Rich Hamilton, called “Category Visibility” (based on code written by Keith McDuffee.

    Here’s the link.

    RYO ‘Category Visibility’ WordPress Plugin

    Mr. McDuffee’s version originally included a limitation where 10 posts were queried, but if two of them were hidden, only 8 posts were returned. I was having the same problem – If one post was queried, but one of them was marked as hidden, 0 posts were returned. However, Mr. Hamilton’s excellent work has solved the problem.

    Another shining example of the WordPress community.

    Good to hear it worked out for you!
    Happy blogging 🙂

    WOW Moshu – 2 years at these forums? You must be a masochist. :>)
    Congrats as well.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘“Single post” combined with “Exclude Categories”’ is closed to new replies.