SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem when displaying posts in sidebar IN a page.php file

    Hi I have my page.php configured to include a sidebar which consists of different posts. However if i put the sidebar include tag before i start the loop of the page, then the page's content is not displayed.

    You can see the functioning page here: http://tinyurl.com/ykjpglz

    Here the sidebar is on the right as it is being called AFTER the loop of the main content, but i would want it to display on the left hand side and thus being called BEFORE the loop of the main content.

    Here is the sidebar.php code

    Code:
    				<div id="sidebar">
    					<div id="sidebar-top"></div>
    						<h2>Latest News</h2>
    						<?php
    							// hold the current query results in a temp variable
    							$temp_query = $wp_query;
    							$count=0;
    							$posts_per_page=2;
    							query_posts('cat=3&posts_per_page='. $posts_per_page ); ?>
    						<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    							<h3><?php the_title(); ?></h3>
    							<p><?php the_content(); ?></p>
    						<?php
    							$count++;
    							if ($count < $posts_per_page) { ?> 
    							<div class="hr"><hr /></div>
    						<?php } ?>
    						<?php endwhile; endif; ?>
    						
    						<?php get_a_post(25); ?>
    							<h2><?php the_title(); ?></h2>	
    							<?php the_content(); ?>
    						<?php get_a_post(29); ?>
    							<h2><?php the_title(); ?></h2>	
    							<?php the_content(); ?>	
    							<?php
    								global $temp_query;
    								// now back to our regularly scheduled programming
    								$wp_query = $temp_query;
    							?>
    					<div id="sidebar-bottom"></div>
    				</div> <!-- sidebar -->
    and the page.php which would display the way i want, but isn't working. If I do it like this the main content displays nothing

    Code:
    <?php get_header(); ?>		
    			<div id="content">
    				<?php get_sidebar(); ?>
    				
    				<div id="content-main">					
    					<div class="content-box">
    						<div class="content-box-top"></div>
    						<div class="content-box-middle">
    							<?php if (have_posts()) : while (have_posts()) : the_post(); ?>						
    								<h2><?php the_title(); ?></h2>
    								<div class="content-box-middle-gradient">					
    									<?php the_content(); ?>									
    								</div> <!-- content-box-middle-gradient -->
    							<?php endwhile; endif; ?>											
    						</div> <!-- content-box-middle -->
    						<div class="content-box-bottom"></div>
    					</div> <!-- content-box -->
    
    				</div> <!-- content-main -->
    			
    			</div> <!-- content -->
    			
    <?php get_footer(); ?>




    and the page.php file which works (but does not display the way i want)

    Code:
    <?php get_header(); ?>		
    			<div id="content">
    				
    				
    				<div id="content-main">					
    					<div class="content-box">
    						<div class="content-box-top"></div>
    						<div class="content-box-middle">
    							<?php if (have_posts()) : while (have_posts()) : the_post(); ?>						
    								<h2><?php the_title(); ?></h2>
    								<div class="content-box-middle-gradient">					
    									<?php the_content(); ?>									
    								</div> <!-- content-box-middle-gradient -->
    							<?php endwhile; endif; ?>											
    						</div> <!-- content-box-middle -->
    						<div class="content-box-bottom"></div>
    					</div> <!-- content-box -->
    
    				</div> <!-- content-main -->
    			<?php get_sidebar(); ?>
    			</div> <!-- content -->
    			
    <?php get_footer(); ?>
    Anyone help me out please? this is probably a loop issue, but I don't know so much yet to be able to troubleshoot it.

    thanks

  2. #2
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I've been playing around and managed to solve it, i dont know if its the most elegant solution but here is the sidebar.php that enables me to work as i want.

    sidebar.php

    Code:
    				<div id="sidebar">
    					<div id="sidebar-top"></div>
    						<h2>Latest News</h2>
    
    						<!-- Displays Latest News in the sidebar -->						
    						<?php										
    							$count=0;
    							$posts_per_page=2;												
    							$news_query = new WP_Query('cat=3&showposts='. $posts_per_page ); 
    							
    							if ($news_query->have_posts()) : while ($news_query->have_posts()) : $news_query->the_post(); ?>
    								<h3><?php the_title(); ?></h3>
    								<?php the_content(); ?>
    								<?php
    									$count++;
    									if ($count < $posts_per_page) { ?> 
    										<div class="hr"><hr /></div>
    									<?php } ?>
    							<?php endwhile; endif; ?>
    
    							<!-- Displays Menus in the sidebar -->		
    							<?php
    							$menus_query = new WP_Query('p=25'); 
    							
    							if ($menus_query->have_posts()) : while ($menus_query->have_posts()) : $menus_query->the_post(); ?>
    								<h2><?php the_title(); ?></h2>
    								<?php the_content(); ?>
    							<?php endwhile; endif; ?>
    
    							<!-- Displays Restaurant info in the sidebar -->
    							<?php
    							$restaurant_query = new WP_Query('p=29'); 
    							
    							if ($restaurant_query->have_posts()) : while ($restaurant_query->have_posts()) : $restaurant_query->the_post(); ?>
    								<h2><?php the_title(); ?></h2>
    								<?php the_content(); ?>
    							<?php endwhile; endif; ?>							
    														
    					<div id="sidebar-bottom"></div>
    				</div> <!-- sidebar -->


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •