WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Only getting 1 post in sidebar - need 2 (5 posts)

  1. webheadmusic
    Member
    Posted 1 year ago #

    My sidebar.php file has the following code. The problem is that only 1 post is being returned. Any help is greatly appreciated.

    <aside>
    
    		<?php get_search_form(); ?>
    
    		<nav>
    			<?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?>
    		</nav>
    
    		<div class="widget latest-post">
    
    			<h4>Latest Post</h4>
    
    			<?php query_posts("post_per_page=2"); the_post(); ?>
    
    			<div class="sidebar-post">
    				<p class="date"><?php the_date(); ?></p>
    				<h5><?php the_title(); ?></h5>
    				<p><?php the_content(); ?></p>
    			</div>
    
    			<?php wp_reset_query(); ?>
    
    		</div>
    		<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Widgets')) : else : ?>
    
    		    <!-- All this stuff in here only shows up if you DON'T have any widgets active in this zone -->
    
    		<?php endif; ?>
    
    	</aside>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

  3. webheadmusic
    Member
    Posted 1 year ago #

    Thanks very much. I have now added the s in posts_per_page. I'm still baffled though. I have 2 published posts and I want them to both show up in the sidebar. Only one is showing up.

    my_site

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    my mistake - I stopped checking after the first spotted error;

    actually your code does not have 'the loop', i.e. the while( have_posts() ) ... endwhile is missing;

    change to:

    <?php query_posts("posts_per_page=2"); while( have_posts() ) : the_post(); ?>
    
    			<div class="sidebar-post">
    				<p class="date"><?php the_date(); ?></p>
    				<h5><?php the_title(); ?></h5>
    				<p><?php the_content(); ?></p>
    			</div>
    
    			<?php endwhile; wp_reset_query(); ?>
  5. webheadmusic
    Member
    Posted 1 year ago #

    Awesome! You totally rock! That's perfect. Two posts in sidebar. Thank you so much.

Topic Closed

This topic has been closed to new replies.

About this Topic