Forums

Showing previous post on homepage (11 posts)

  1. bswinnerton
    Member
    Posted 2 years ago #

    Hi everyone,

    I'm currently using the structured theme (white). On the page there are two sections, a top and bottom both of which unfortunately show the same post with a picture associated with the post.

    How can I make it so that the top half displays the latest post, and the bottom half displays the post previous to the latest one?

    Here's the code for my home.php in the theme folder:

    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="homepagetop">
    
            <div class="textbanner">
    			<?php $recent = new WP_Query("cat=" .st_option('hp_top_cat'). "&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
                <h3><a href="http://www.sanshoopla.com/blog/about/" rel="bookmark">Thoughts on current happenings from
    an aspiring journalist. </a></h3>
            </div>
    
            <div class="featuredimage">
            	<?php if( get_post_meta($post->ID, "featured", true) ): ?>
            	<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "featured", true); ?>" alt="<?php the_title(); ?>" /></a>
                <?php else: ?>
                <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/images/default_featured.jpg" alt="<?php the_title(); ?>" /></a>
                <?php endif; ?>
                <?php endwhile; ?>
            </div>
    
            <div class="homewidgets">
    
                <ul>
                <h4><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h4>
                    	<?php the_content_limit(480, ""); ?>
                </ul>
    
            </div>
    
        </div>
    
        <div id="homepage">
    
        	<?php include(TEMPLATEPATH."/sidebar_left.php");?>
    
            <div class="homepagemid">
    
    			<h3><!--<?php echo cat_id_to_name(st_option('hp_mid_cat')); ?>--></h3>
    
    			<?php $recent = new WP_Query("cat=" .st_option('hp_mid_cat'). "&showposts=" .st_option('hp_mid_num') ); while($recent->have_posts()) : $recent->the_post();?>
    
                	<div class="homepagethumb">
                    	<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
                    	<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
                        <?php else: ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/images/default_thumbnail.jpg" alt="<?php the_title(); ?>" /></a>
                        <?php endif; ?>
                    </div>
    
                	<div class="homepagecontent">
    
                        <h4><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h4>
                    	<?php the_content_limit(480, ""); ?>
    
                    </div>
    
                <?php endwhile; ?>
    
            </div>
    
            <?php include(TEMPLATEPATH."/sidebar_right.php");?>
    
    	</div>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
  2. mattrogers123
    Member
    Posted 2 years ago #

    can you post a link to your site?

  3. bswinnerton
    Member
    Posted 2 years ago #

    s a n s h o o p l a [dot] c o m

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

  5. bswinnerton
    Member
    Posted 2 years ago #

    Hmm, I stumbled upon the WP_Query that you posted, but I couldn't figure out the correct syntax for the function. Since there is nothing actually calling $id, I can't just do $id-1. Is there any other suitable way to do it?

  6. alchymyth
    The Sweeper
    Posted 2 years ago #

    the second link i gave you, was wrong;

    i tried to point to the

    'post__not_in' => array(6,2,8) - exclusion, lets you specify the post IDs NOT to retrieve
    parameter for the query.
    http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters

    so, in your first loop you would save the post id in a variable, and then use this variable in the second loop with the 'post__not_in' parameter to exclude the first post.

  7. bswinnerton
    Member
    Posted 2 years ago #

    Hmm, I'm sorry I'm just not familiar with the query. I understand that I'll have to save the post ID, but how can I do that inside the loop? What is the name of the current postid?

    For example inside the first look I would do something like the following:

    $id = current_post_id();

    ^ I just don't know the way to call the current post id.

  8. alchymyth
    The Sweeper
    Posted 2 years ago #

    in the first loop you could get the id of the post:

    $id[] = get_the_ID();

    http://codex.wordpress.org/Function_Reference/get_the_ID

    and then rewrite the code for the second query:

    <?php $args = array(
    'post__not_in' => $do_not_duplicate,
    'cat' => st_option('hp_mid_cat'),
    'showposts' => st_option('hp_mid_num'));
    
    $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>

    http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    untested, good luck ;-)

  9. bswinnerton
    Member
    Posted 2 years ago #

    I can't seem to get it to work. I've been trying versions of the code, but I just can't get it.

    I've tried:

    <?php $recent = new WP_Query("cat=" .st_option('hp_top_cat'). "&showposts=1"); while($recent->have_posts()) : $recent->the_post(); $id[] = get_the_ID();?>

    But can't echo even echo the id.

  10. alchymyth
    The Sweeper
    Posted 2 years ago #

    i think i had some variables mixed up in my code;

    this here is kind-of-tested:
    you could try this:

    first loop; from your original code:

    <?php $recent = new WP_Query("cat=" .st_option('hp_top_cat'). "&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

    change to this, including the array to catch the posts id of the first loop:

    <?php $recent = new WP_Query("cat=" .st_option('hp_top_cat'). "&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
    <?php	$do_not_duplicate[] = $post->ID; ?>

    then, the second loop; from your original code:

    <?php $recent = new WP_Query("cat=" .st_option('hp_mid_cat'). "&showposts=" .st_option('hp_mid_num') ); while($recent->have_posts()) : $recent->the_post();?>

    to this changed code, to exclude the posts from the first loop:

    <?php $args = array(
    'post__not_in' => $do_not_duplicate,
    'cat' => st_option('hp_mid_cat'),
    'showposts' => st_option('hp_mid_num')
    );
    
    $recent = new WP_Query($args) ; while($recent->have_posts()) : $recent->the_post();?>

    hope this works now, good luck

  11. bswinnerton
    Member
    Posted 2 years ago #

    Fantastic!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.