• I am using the Twenty-Twelve theme and have created a child theme from it and a “Page of Posts” template. I have included the template below. Does anyone know how I can modify the template to make the “page of posts” display only one post (the most recent)? Right now it displays all posts in category “news.” Thanks for any help.

    <?php
    /**
     * Template Name: Page of Posts
     *
     * for a child theme of Twenty_Twelve
     */
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    
    		<?php
    			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    			$args= array(
    				'category_name' => 'news', // Change this category SLUG to suit your use; or see for query parameters http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
    				'paged' => $paged
    );
    			query_posts($args);
    			if( have_posts() ) :?>
    
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    				<?php comments_template( '', true ); ?>
    			<?php endwhile; // end of the loop. ?>
    
    			<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    			<?php else : ?>
    			<article id="post-0" class="post no-results not-found">
    				<header class="entry-header">
    					<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
    				</header>
    				<div class="entry-content">
    					<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
    					<?php get_search_form(); ?>
    				</div><!-- .entry-content -->
    			</article><!-- #post-0 -->
    
    			<?php endif; wp_reset_query(); ?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter chaplaindoug

    (@chaplaindoug)

    That article is about showing posts from a single category. My template already does that. I desire to show ONE POST only on the page.

    Your code seems to be missing a closing paranthesis for args variable. Assign the following to $args. Hope that helps.

    $args= array(	'paged' => $paged,
    'numberposts' => '1'
    )
    Thread Starter chaplaindoug

    (@chaplaindoug)

    Thanks Adity. But the addition of that parameter (argument) made no difference. The page still displays all the posts. Any other ideas?

    By the way, the parenthesis is there, just on the next line.

    Thread Starter chaplaindoug

    (@chaplaindoug)

    This worked:

    ‘posts_per_page’ => ‘1’

    However, I then discovered that the “Older Posts” link that shows at bottom of page although linked to a different URL, simply displays the same one post again. This was a problem I thought was due to a plugin (Posts-In-Page), which led me to try the template approach. I now see that it is a problem with the query_posts function.

    Thread Starter chaplaindoug

    (@chaplaindoug)

    I found an article that gave the fix for the problem noted above.

    http://www.sunnybalanga.com/2011/07/07/wordpress-query_posts-page-navigation-problem-fixed-solved/

    I had the line indicated. But it had “paged” instead of “page.” With that correction the “Older Posts” link works.

    My modified template files is as follows:

    <?php
    /**
     * Template Name: Page of Posts
     *
     * for a child theme of Twenty_Twelve
     */
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    
    		<?php
    			$paged = (get_query_var('page')) ? get_query_var('page') : 1;
    			$args= array(
    				'category_name' => 'news', // Change this category SLUG to suit your use; or see for query parameters http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
    				'paged' => $paged,
                         'posts_per_page' => '1');
    			query_posts($args);
    			if( have_posts() ) :?>
    
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    				<?php comments_template( '', true ); ?>
    			<?php endwhile; // end of the loop. ?>
    
    			<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    			<?php else : ?>
    			<article id="post-0" class="post no-results not-found">
    				<header class="entry-header">
    					<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
    				</header>
    				<div class="entry-content">
    					<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
    					<?php get_search_form(); ?>
    				</div><!-- .entry-content -->
    			</article><!-- #post-0 -->
    
    			<?php endif; wp_reset_query(); ?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to Display Just One Post on a Page of Posts’ is closed to new replies.