• 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

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter bmerikal

    (@bmerikal)

    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

    Thread Starter bmerikal

    (@bmerikal)

    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

    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…

    Thread Starter bmerikal

    (@bmerikal)

    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

    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!

    Thread Starter bmerikal

    (@bmerikal)

    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

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

    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

    10sexyapples

    (@10sexyapples)

    Thank you t31os_, I am using your code fix on several of my templates to show one post on first page of the archive with custom styling and then 4 posts_per_page on the following archive pages. Everything works fine for page one and page two of the archive, but then it won’t page past page 2 for some reason. I’ve posted this issue in several places here, and have spent at least a weeks time trying to fix this and haven’t been able to come up with anything at all. I figured you might be able to enlighten me as to what is causing this and help me get it sorted. It’s really got me pulling my hair out, so, any help at all would be GREATLY appreciated … from anyone.

    Here is my code:

    <div id="content" class="fullpage clearfix color-block">
    
    <?php $page_title = category_description("", false); ?>
    
    	<h3 class="pagetitle float"><?php echo $page_title; ?></h3>
    
    <?php
    $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=4';
    query_posts("paged=$paged&cat=$cat&offset=$offset&$show");
    ?>
    
    <?php if ( $paged < 2 ) { ?>	
    
    <div id="nav"><?php posts_nav_link(' ', '<img src="' . get_bloginfo('template_url') . '/images/navigation/larrow.png" />', '<img src="' . get_bloginfo('template_url') . '/images/navigation/rarrow.png" />'); ?></div>
    <div class="clear"></div>
    
        <div id="towerL">
    		<?php slidepress_display_gallery('');?>
        </div><!--close towerL-->
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<div id="towerR">
    		<div class="post" id="post-<?php the_ID(); ?>">
                    <div class="entry">
    			<?php the_content ("Continue reading " . the_title('', '', false)); ?>
    			<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    		</div> <!--close entry-->
                    </div> <!--close post-->
    
    		<?php endwhile; endif; ?>
    
    		</div><!--close TowerR-->
    
    	<div id="bottom-block">
    	<div class="draggable-header">/stuff/</div>
            </div><!--close bottom-block-->       
    
    <?php } else { ?>
    
    <div id="nav"><?php posts_nav_link(' ', '<img src="' . get_bloginfo('template_url') . '/images/navigation/larrow.png" />', '<img src="' . get_bloginfo('template_url') . '/images/navigation/rarrow.png" />'); ?></div>
    <div class="clear"></div>
    
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
                <div class="block_big">
    
                <div class="boxgrid caption">
                <?php if ( function_exists( 'get_the_image' ) ) { get_the_image( array( 'custom_key' => array( 'Exhibition_Image' ), 'default_size' => 'full', 'width' => '450', 'height' => '190', 'default_image'=> get_bloginfo('wpurl') . '/wp-content/uploads/Defaults/defaultexhibitionimage.jpg', 'image_class' => 'exhibitionimage' ) ); } ?>
                <div class="cover boxcaption">
    
                <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <div class="post" id="post-<?php the_ID(); ?>">
    		<div class="entry">
                <p><?php excerpt('57'); ?><p>
                </div><!--close entry-->
                </div><!--close post-->
                </div><!--close cover boxcaption-->
                </div><!--close boxgrid-->
    
                </div><!--close block-->
    
            <?php endwhile; ?>
    
    <?php endif; ?>
    
    <?php } ?>
    
    </div><!--close content-->
    </div> <!--close innerwrap-->
    <?php get_footer(); ?>

    Cheers guys, a few things here helped me solve some problems I was having.

    I’m having trouble getting this to work for some reason in 2.9.2. I have a category-something.php file and right before the Loop, I have the following:

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

    But when I go to other pages in this category, it’s still only showing 1 post on each page. Am I doing something wrong? I only wanted to have 1 post on the first page of the category and the rest of the category subpages should show as many posts as I set in Settings>Reading (which is currently 5).

    Thanks! ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Is it possible to show 1 post on category main page and then the rest on p2?’ is closed to new replies.