• Resolved NewYorkCityZoo

    (@newyorkcityzoo)


    When the user clicks on a post (on the homepage) it takes them to the single.php template where I’d like it to display that post in detail and the previous 3 below. This sounds easy but has been quite a hassle. I need to know how to display the previous posts in reference to the current post.

    Here is the code for the single.php page:


    <?php
    /**
    * @package WordPress
    * @subpackage WPG_Theme
    */
    get_header();
    ?>

    <div id="contentArea">
    <table cellspacing="0">
    <tr>
    <td>
    <div id="leftColumn">

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class="blogPosts">
    <h1>Latest from WPG</h1>
    <div id="latestBlog">
    <!--<img src="wp-content/themes/wpg/images/blog-photo.jpg" width="163" height="140" alt="" />-->
    <h2>" rel="bookmark"><?php the_title(); ?></h2>
    <h3>Published <?php the_time('F jS, Y') ?></h3>
    <p><?php the_content(); ?></p>
    <p><?php wp_list_comments(); ?></p>
    </div>
    </div>

    <?php comments_template(); ?>
    <?php endwhile; else: ?>

    <p>Sorry, no posts matched your criteria.</p>

    <?php endif; ?>

    <!-- Next 3 Blog Posts -->
    <div class="blogPosts">
    <h1>Older Posts</h1>
    <table cellspacing="0" class="postGrid">
    <tr class="noDots">
    <th class="column1">TITLE</th>
    <th class="column2">Comments</th>
    <th class="column3">Last updated</th>
    </tr>

    <!--query_posts('posts_per_page=3','orderby=asc');-->

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <tr>
    <td class="column1">
    <img src="wp-content/themes/wpg/images/grid-photo.jpg" width="68" height="62" alt="" />
    <h4>" rel="bookmark"><?php the_title(); ?></h4>
    <p>Published: <?php the_time('m/d/Y') ?></p>
    <p>Summary: <?php echo get_the_excerpt(); ?></p>
    </td>
    <td class="column2">"><?php comments_number('0','1','%'); ?></td>
    <td class="column3"><?php the_modified_date(); ?></td>
    </tr>

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    </table>
    <div class="rightArrowLink">View All Posts</div>
    </div>

    </div> <!-- End Left Column -->
    </td>
    <td>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I have been searching for a solution everywhere. No luck yet.

    I have researched query_posts and <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> loop methods and have not found the option to list previous by id or current post.

    Maybe there is a plugin or snippit I’ve overlooked but hopefully someone has had this problem before. Thanks for any help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter NewYorkCityZoo

    (@newyorkcityzoo)

    Horrible code formatting…sorry

    Thread Starter NewYorkCityZoo

    (@newyorkcityzoo)

    Ahhhh freshnesss.

    Thread Starter NewYorkCityZoo

    (@newyorkcityzoo)

    Thread Starter NewYorkCityZoo

    (@newyorkcityzoo)

    Solution:

    <?php
    /**
     * @package WordPress
     * @subpackage WPG_Theme
     */
    get_header();
    ?>
    <script language="JavaScript" type="text/javascript">
    	theSection = "blog";
    </script>
    
    	<div id="contentArea">
    		<table cellspacing="0">
    			<tr>
    				<td>
    				<div id="leftColumn">
    
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    					<div class="blogPosts">
    						<h1>Latest News</h1>
    						<div id="latestBlog">
    							<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    							<h3>Published <?php the_time('F jS, Y') ?></h3>
    							<p><?php the_content(); ?></p>
    							<p><?php wp_list_comments(); ?></p>
    						</div>
    					</div>
    					<?php $currentPostID = $post->ID; ?>
    					<?php comments_template(); ?>
    					<?php endwhile; else: ?>
    
    					<p>Sorry, no posts matched your criteria.</p>
    
    				<?php endif; ?>
    
    					<!-- Next 3 Blog Posts -->
    					<div class="blogPosts">
    						<h1>Previous News</h1>
    						<table cellspacing="0" class="postGrid">
    							<tr class="noDots">
    								<th class="column1">TITLE</th>
    								<th class="column2">Comments</th>
    								<th class="column3">Last updated</th>
    							</tr>
    							<?php
    
    							// Custom Query to get 3 Previous Post by the ID of the current post (above)
    							$querystr = "
    								SELECT *
    								FROM wp_posts
    								WHERE ID < $currentPostID
    								AND post_status = 'publish'
    								ORDER BY ID DESC
    								LIMIT 3;
    							";
    
    							$pageposts = $wpdb->get_results($querystr, OBJECT);
    
    							if ($pageposts):
    								foreach ($pageposts as $post):
    								setup_postdata($post); 
    
    								?>
    
    								<tr>
    									<td class="column1">
    										<!--<img src="wp-content/themes/wpg/images/grid-photo.jpg" width="68" height="62" alt="" />-->
    										<h4><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h4>
    										<p><strong>Published:</strong> <?php the_time('m/d/Y') ?></p>
    										<p><strong>Summary: </strong><?php echo get_the_excerpt(); ?></p>
    									</td>
    									<td class="column2"><a href="<?php comments_link(); ?>"><?php comments_number('0','1','%'); ?></a></td>
    									<td class="column3"><?php the_modified_date(); ?></td>
    								</tr>
    
    								<?php 
    
    								endforeach; 
    
    							else : 
    
    							?><p class="center">No posts to display.</p><?php 
    
    							endif; 
    
    							?>
    
    						</table>
    						<div class="rightArrowLink"><a href="<?php echo get_permalink(19); ?>">View All Posts</a></div>
    					</div>
    
    				</div> <!-- End Left Column -->
    				</td>
    				<td>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to display current post on single.php and the previous 3’ is closed to new replies.