Support » Fixing WordPress » Number of posts on home page vs. category pages

  • Hi,

    I know in the control panel you can set the amount of posts on a page to be a certain number. However, I want the HOME page to only display 1 post – then the internal category pages to display more. How can I do this?

Viewing 14 replies - 1 through 14 (of 14 total)
  • This tutorial on home.php might be helpful.

    Combine that with query_posts(‘posts_per_page=1’), and you should be able to do what you want.

    Thread Starter tdellaringa

    (@tdellaringa)

    There is no home.php in any of the themes (I made my theme by copying and then modifying). Is this a page I have to add?

    (Moshu thanks for the plugin links. I’m wary of using plug-ins for a variety of reasons – upkeep being one. I’d rather customize my own code)

    Yes. See Template Hierarchy for additional information about the template files you can use. Not all templates use all template files.

    Thread Starter tdellaringa

    (@tdellaringa)

    Ok, so is there a reason why I cannot use the query_posts(‘posts_per_page=1’) in the index.php instead?

    Seems it would go somewhere in here…?

    <?php if (have_posts()) : ?>

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

    As long as you have seperate template files for each of:
    * archives
    * authors
    * categories
    * pages
    * single posts
    then you can do what you want in index.php.

    If your templates use index.php for their Loop, you’ll need to use a conditional tag, like is_home(), to make sure you’re only adjusting the query when viewing the home page.

    <?php
    if (is_home()) {
    query_posts('posts_per_page=1');
    }
    // begin the loop here...

    If using pagination on your home page (next/previous page links), make this change to skippy’s code above:

    <?php
    if (is_home()) {
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("posts_per_page=1 & paged=$page");
    }
    // begin the loop here...

    That will assure you page through the posts corrrectly.

    Thread Starter tdellaringa

    (@tdellaringa)

    When you say ‘loop’ do you mean the have_posts()?

    Also, doing this in my index.php page – I get an 404. It wants to look for domain/home – maybe I am putting it in the wrong place…

    See The Loop for clear documentation about what we mean when we say “The Loop”.

    Thread Starter tdellaringa

    (@tdellaringa)

    Edit, nevermind I got it. Thanks everyone.

    You want the is_home() check to go before you begin the Loop.

    The query_posts() function is modifying the selection of posts to use in the Loop. Because you only want the home page to be handled differently, you need to set up the Loop differently.

    Note in my original example the PHP comment:
    // begin the loop here...
    It is after that comment that you would begin the Loop.

    Thread Starter tdellaringa

    (@tdellaringa)

    Thanks, Skippy for the clarification. 🙂 Now, the one post thing works until I click on the ‘previous’ link – I get the previous article, but it has no content nor summary…any idea why? Here is how I implemented it:

    <?php if (is_home()) {
    $page = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    query_posts(“posts_per_page=1 & paged=$page”);
    }

    if (have_posts()) : ?>

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

    <div class=”post”> etc etc…

    (Example: http://www.crossandthrone.com/page/2/ )

    This “Custom Query String” Plugin is really cool, and easy to use. It works on my home page and search results. It works on category in general, but I can’t get it working on specific category ID’s. Is it because I’m using permalinks?

    I have a problem here too: http://www.cinencuentro.com/
    I first used the standard option from WP. I set 3 post per page, because that’s what I wanted for the home page. Then I realized I’d need like 10 or 20 posts per page for the Search results or the Categories page. So I installed “Custom Query String” Plugin. I set the number of pages for Search Results to 10, and it worked, but for some reason the main page showed 4 posts. If I set a number for a Category page, it wont work either. And it got worse: I deleted all the rules/conditions of the plug-in, and I’m still getting the same results, as if the rules will still be set. Could it be related to somme conflict with other plug-in?
    Only if I disactive the plug-in it will stop using the (deleted) rules/conditions.
    I really need this feature, it is a great idea. I hope someone can give me some help.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Number of posts on home page vs. category pages’ is closed to new replies.