Forums

Number of posts on home page vs. category pages (15 posts)

  1. tdellaringa
    Member
    Posted 6 years ago #

    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?

  2. moshu
    Member
    Posted 6 years ago #

  3. skippy
    Member
    Posted 6 years ago #

    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.

  4. tdellaringa
    Member
    Posted 6 years ago #

    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)

  5. skippy
    Member
    Posted 6 years ago #

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

  6. tdellaringa
    Member
    Posted 6 years ago #

    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(); ?>

  7. skippy
    Member
    Posted 6 years ago #

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

  8. Kafkaesqui
    Moderator
    Posted 6 years ago #

    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.

  9. tdellaringa
    Member
    Posted 6 years ago #

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

  10. skippy
    Member
    Posted 6 years ago #

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

  11. tdellaringa
    Member
    Posted 6 years ago #

    Edit, nevermind I got it. Thanks everyone.

  12. skippy
    Member
    Posted 6 years ago #

    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.

  13. tdellaringa
    Member
    Posted 6 years ago #

    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/ )

  14. Dgold
    Member
    Posted 6 years ago #

    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?

  15. laslo
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags