• For my index.php i only just wanna show a few categories from my usual line-up of posts. I am using other categories to put posts as well, but i only wanna show three categories on the normal index.php.

    To show those I am using the following code:

    <?php $posts=query_posts($query_string . '&posts_per_page=5'); ?>
    	<?php if(have_posts()) { while ( have_posts() ) : the_post();
    		if(in_category('film') || in_category('wip') || in_category('forum')) { ?>
    // Loop-y-loop stuff
    	<?php }; endwhile; } else { echo 'Sorry, no updates yet.'; } ?>

    Now the issue here is that even though it’s showing just those categories I have selected it is still showing 4 posts on the every page. Meaning, in between there’s another post that’s not in the specified categories, but it’s still taking up a place in the posts_per_page string.

    How can I solve this issue so the index.php will still be showing 5 posts per page, ignoring any other posts that are not in the specified categories?

Viewing 1 replies (of 1 total)
  • What is happening here is that you are telling WP to get the most recent 5 posts from any category from the database. From there, you are telling WP to only use the ones from those three categories. Depending on how you’ve been posting lately, you may get all 5 or even no posts showing. What you want to do is tell WordPress to go to the database and only get the posts that are in those three categories. Here’s how:

    <?php $posts=query_posts($query_string . '&posts_per_page=5&cat=1,2,3'); ?>
    	<?php if(have_posts()) { while ( have_posts() ) : the_post();
    // Loop-y-loop stuff
    	<?php }; endwhile; } else { echo 'Sorry, no updates yet.'; } ?>

    Where I have cat=1,2,3 you’re going to want to replace those with the category ID number for your ‘film’, ‘wip’, and ‘forum’ categories.

Viewing 1 replies (of 1 total)
  • The topic ‘Specific category on index.php but empty on other categories’ is closed to new replies.