WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Over-ride "Posts per Page using special coding (5 posts)

  1. Bladefallcon
    Member
    Posted 2 years ago #

    I am using coding I found and modified that adds the content of posts to the end of a page, where the category of the post matches the name of the page. In other words, The Page is called "Test" So all posts with the category "Test" will be added at the end of the page.

    This allows me to Have a quick introduction about the content of the posts, before showing the posts. (the original code included the name of the posts as well, but I did not need it, so I took it out.)

    The problem is this: One the rest of the site, I want only the most recent post to show up on any given page. But on pages that use this template, I want all the posts (or at least the last 25 or so) to show up. So what do I have to add to this code to do that?

    <?php /*
    Template Name: CategoryPostPage
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    <div id="main">
    
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        	<p><?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    </div>
    </div>
    
    <?php get_footer(); ?>

    I tried adding 'showposts=25'
    or
    'posts_per_page=25'

    I assumed they should be added within the query_post() parameters, but that doesn't seem to be working. I tried adding a separate Query_post() function, but that seemd to just override the first, and list every post out there...

    What am I missing?

  2. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    The biggest problem here is that you're using query_posts() for something other than altering the main loop query.

    For a secondary loop, use WP_Query() instead.

    For example:

    $cat_posts_query_args = array(
        'category_name' => get_the_title(),
        'post_status' => array( 'publish', 'future' ),
        'posts_per_page' => 25
    );
    $cat_posts_query = new WP_Query( $cat_posts_query_args );

    Then, you just set up your secondary loop:

    if ( $cat_posts_query->have_posts() ) : while ( $cat_posts_query->have_posts() ) : $cat_posts_query->the_post();
        // Secondary loop output goes here
        the_content();
    endwhile; endif;
    //Be kind; rewind
    wp_reset_postdata()
  3. Bladefallcon
    Member
    Posted 2 years ago #

    Awesome! That worked perfectly. Thanks man. Like I said, the code I was using was something I found, written by someone else. So I didn't know if what I was using was the best way to go or not. Your code worked great!

    For those who find this posting looking for a solution to the same basic situation, here is the new code that works fine.

    <?php /*
    Template Name: CategoryPostPage
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    <div id="main">
    
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
        // If you want more or less posts to be displayed,
        // change '25' to the number desired.
    	<?php $cat_posts_query_args = array(
        'category_name' => get_the_title(),
        'post_status' => array( 'publish', 'future' ),
        'posts_per_page' => 25
    );?>
    <?php $cat_posts_query = new WP_Query( $cat_posts_query_args ); ?>
    
    <?php if ( $cat_posts_query->have_posts() ) : while ( $cat_posts_query->have_posts() ) : $cat_posts_query->the_post();
        the_content();
    endwhile; endif;
    wp_reset_postdata() ?>
    </div>
    </div>
    
    <?php get_footer(); ?>

    For those who find this, but are even more new at this stuff than me, take the above code, and save it as page.php or anythingelse.php
    Then, create a page (with the same name as the category of posts you want to display) with some text intorducing the posts. Select "CategoryPostPage" as the page's template.

    The page will now display the text you entered, and then the most recent 25 posts in the category of the same name as the page.

  4. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Glad to help!

    (Please be sure to mark the topic as "resolved", if it is resolved. :) )

  5. Bladefallcon
    Member
    Posted 2 years ago #

    Also, any who would like to see this code in action, can visit the website I am currently using it for. It is a website for my church. I wanted a page that would give some information about how to listen to our recorded Sermons, And then I wanted to list the sermons.

    Rather than edit the page every week with a new link, I wanted to just add a new post which would have nothing but the link.

    So, each week, I add a new post, the text of which is simply a link to the mp3 for that week. It is automatically added to the bottom of the page.

    The site is http://www.broadwaybaptistchurch.net
    go to the Sermons page to see this code in action.

Topic Closed

This topic has been closed to new replies.

About this Topic