Forums

Is it possible to show 1 post on category main page and then the rest on p2? (9 posts)

  1. bmerikal
    Member
    Posted 2 months ago #

    Hi,

    I'm new to WP and PHP, but have managed to cobble together something that's almost doing what I want: I have a "Travels" Category and I would like to show the most recent Post on /travels/ and then show excerpts from all the Travels posts on /travels/page/2.

    Right now, I have this before the Loop (in my category-7.php file which is the template for this Category):

    $posts = query_posts($query_string . '&orderby=date&order=des&posts_per_page=-1');

    Then I start the loop and then have this:

    <!--Intro section for just 1st page of each category-->
    <?php if ( $paged < 2 ) { ?>
       Intro text and hand-picked posts to highlight

    Followed by this:

    <?php } else { ?>
    <!--Once you get past the first page, you get the list of all posts-->
    
    <ul class="posts">
      <?php while (have_posts()) : the_post(); ?>
      <li>
        <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
        <?php the_excerpt() ?>
        <cite>Excerpt from “<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>”</cite>
      </li>
      <?php endwhile; ?>
    </ul>

    Is it possible to show just the most recent post (or two or four) on the main Category page, but still show all of the Travels posts on page 2 (or show 30 posts at a time, etc.) on subsequent pages within this Category?

    Thanks,

    Bill

  2. bmerikal
    Member
    Posted 2 months ago #

    Actually, I suppose my question should be:

    How do I show X number of posts on a Category main page and Y number of posts on that Category's subpages?

    Is there a plug-in for this? (I'm not looking to show a different number of posts in different Categories, but rather within the same Category.)

    Is this possible? Anyone?

    Thanks,

    Bill

  3. bmerikal
    Member
    Posted 2 months ago #

    Anyone?

    Is it possible to show X number of posts on a Category main page and then show Y number of Posts on that Category's subpages?

    I'm stymied. Any and all help, even if it's just, "Nope" is appreciated.

    Thanks,

    Bill

  4. jessn
    Member
    Posted 2 months ago #

    Well you could set up two different page templates. One for your main category page that only queries the first post, and then a second template for your sub pages that queries all posts with an offset of one to hide the first post. Then link page one to page two...

  5. bmerikal
    Member
    Posted 2 months ago #

    Hi, jessn,

    Thanks for taking the time to help.

    How do I do that if my travels Category is ID7? In other words, how do I make WP only load say 5 posts on the main travels page, and then load 30 on subsequent pages within the same Category?

    Or rather, how do I make it use category-7.php for the first page and then some other category template for subsequent pages? Or, is there a way to make display a different number of posts using just category-7.php? (I got the all or none taken care of with this bit:
    posts_per_page=-1

    Thanks again,

    Bill

  6. jessn
    Member
    Posted 2 months ago #

    Are you doing this for multiple category pages, or just one? Because if it's just one you can make a page template by opening and saving page.php as something like "page_category7.php". Then paste this at the very very top:

    <?php
    /*
    Template Name: Category 7
    */
    ?>
    Then replace the loop on this page with this:
    <?php query_posts('category_name=recipes&showposts=10'); ?>
    <?php while (have_posts()) : the_post(); ?>
    the title, the content, whatever you want here
    <?php endwhile; ?>

    Replace the category name with your category slug, and change the number of posts to show.

    Repeat that same process but save the template as something like "page_category7_pg2.php" and change the query posts to

    <?php query_posts('category_name=recipes&showposts=100&offset=1'); ?>

    Change the offset to however many posts you have on the first page.

    Not sure how to make it paginate though...

    Then create two new pages in your editor and off to the right there's a drop down box to select your page template. Choose one of those templates that you just made!

  7. bmerikal
    Member
    Posted 1 month ago #

    Thanks for the idea of using a Page instead of trying to force my category template to do what it didn't want to do. Right now it's showing only one post from the 'current' category, but it's showing it twice.

    My code looks this:

    <!--Get only the most recent post from the current journal-->
    <?php query_posts('category_name=current&showposts=1'); ?>
    <!--The Loop-->
    <?php while (have_posts()) : the_post(); ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    			<?php the_title(); ?></a></h2>
    		<p><?php the_time('F jS, Y') ?></p>
    		<?php the_content('Read the rest of this entry &raquo;'); ?>
    	</div>
    <?php endwhile; ?>
    <!--End of The Loop-->
    <a href="/travel-journal/current/">Continue reading current travel journal &raquo;</a>

    Any idea why that might be? (I'm using the Exec-PHP plugin, so that might be the cause of the error.)

    Thanks,

    Bill

  8. t31os_
    Member
    Posted 1 month ago #

    You don't need a page template, that's over-complicating the requirement.

    Stick with cateogory-7.php, and use this for your query_posts area.

    <?php
    $per_page = get_option('posts_per_page');
    $paged = (get_query_var('paged') && get_query_var('paged') > 1) ? get_query_var('paged') : 1;
    $cat = (get_query_var('cat')) ? get_query_var('cat') : 0;
    $offset = ($paged > 1) ? ($per_page * ($paged - 1) + 1) - $per_page : 0;
    $show = ($offset == 0) ? 'posts_per_page=1' : 'posts_per_page='.$per_page;
    query_posts("paged=$paged&cat=$cat&offset=$offset&$show");
    ?>

    Took a little bit of playing around with but the paging works, and you get the correct offset..

    1 posts on the first page, then all others on the additional pages..

  9. sinans11
    Member
    Posted 4 weeks ago #

    see the worst thing about this is that when ever someone resolves a problem no thank you is said. any way thank this might come in handy for me. thanks t31os_ and the other guys here and girls if any

Reply

You must log in to post.

About this Topic