[resolved] "Single post" combined with "Exclude Categories" (9 posts)

  1. jeff_n
    Posted 9 years ago #

    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?

  2. moshu
    Posted 9 years ago #

    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.

  3. jeff_n
    Posted 9 years ago #

    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.

  4. moshu
    Posted 9 years ago #

    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...

  5. jeff_n
    Posted 9 years ago #

    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. :)

  6. moshu
    Posted 9 years ago #

    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

  7. jeff_n
    Posted 9 years ago #

    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.


    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.

  8. moshu
    Posted 9 years ago #

    Good to hear it worked out for you!
    Happy blogging :)

  9. Samuel B
    Posted 9 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic