WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Limit Posts on Homepage (10 posts)

  1. bgajus
    Member
    Posted 3 years ago #

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

  2. DigitalSquid
    Member
    Posted 3 years ago #

    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.

  3. bgajus
    Member
    Posted 3 years ago #

    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

  4. chinmoy
    Member
    Posted 3 years ago #

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

  5. bgajus
    Member
    Posted 3 years ago #

    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?

  6. chinmoy
    Member
    Posted 3 years ago #

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

  7. bgajus
    Member
    Posted 3 years ago #

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

  8. chinmoy
    Member
    Posted 3 years ago #

    but it works for me.

  9. bgajus
    Member
    Posted 3 years ago #

    works when I remove the

    $tmp = $wp_query;
     $wp_query = null;

    and the

    $wp_query = $tmp;

    at the end.
    hrm......

  10. chinmoy
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic