WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Limit Posts on Homepage

[Resolved] Limit Posts on Homepage

  • How do you limit the number of posts shown just on the homepage?
    I just want to show 1 post on the homepage only.

Viewing 9 replies - 1 through 9 (of 9 total)
  • DigitalSquid

    @twelvefootsnowman

    Log into your WordPress control panel and in the left menu go to Settings > Reading. Then change the number next “Blog pages show at most” to 1.

    will that not change all my archive pages to only show 1 as well?…
    just checked and yeah it doesn’t quite do what I want. I need only the homepage to show the single the most recent post. All the rest of my pages need to show using the Blog pages show at most number

    <?php
     $tmp = $wp_query;
     $wp_query = null;
     $wp_query = new WP_Query('showposts=1');
    ?>

    write the above code before this line while(have_posts()): the_post();
    And after end of endwhile; line write this
    $wp_query = $tmp;

    in your home page.

    I’m using the twentyten theme…. on index.php it uses
    get_template_part( 'loop', 'index' );

    Do I need to add that code to the loop.php page?

    <?php
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */
    
    get_header(); ?>
    <?php
     $tmp = $wp_query;
     $wp_query = null;
     $wp_query = new WP_Query('showposts=1');
    ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    
    			<div class="entry-meta">
    				<?php twentyten_posted_on();?>
    			</div><!-- .entry-meta -->
    
    			<div class="entry-content">
    				<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
    				<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    			</div><!-- .entry-content -->
    
    			<div class="entry-utility">
    				<?php if ( count( get_the_category() ) ) : ?>
    					<span class="cat-links">
    						<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<?php
    					$tags_list = get_the_tag_list( '', ', ' );
    					if ( $tags_list ):
    				?>
    					<span class="tag-links">
    						<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    					</span>
    					<span class="meta-sep">|</span>
    				<?php endif; ?>
    				<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
    				<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    			</div><!-- .entry-utility -->
    		</div><!-- #post-## -->
    <?php endwhile; $wp_query = $tmp; // End the loop. Whew. ?>
    </div><!-- #content -->
    		</div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    First keep a backup of index.php file. Then replace with my code

    nothing is displaying to the page after the header. It stops at the start of index.php content.

    but it works for me.

    works when I remove the

    $tmp = $wp_query;
     $wp_query = null;

    and the

    $wp_query = $tmp;

    at the end.
    hrm……

    ok. So your issue is solved. Now mark it as resolved.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Limit Posts on Homepage’ is closed to new replies.
Skip to toolbar