• Resolved agusparadak

    (@agusparadak)


    Hi, I’m totally new with WordPress, and so far I’m doing it ok.
    But there are certain things I want to do that no matter how many forums or documentation I read, I just can’t do it!

    Please, help me on this one:
    I’m creating my theme and in the index.php I’m showing the last post as the “Main news”. Then show in “Other news” the 3 previous posts that have been created. Lets say, I need to show 3 from the penultimate post.

    This is what I have so far in “Other news”:

    <?php
    
    query_posts('showposts=3'); 
    
    if (have_posts()) :
    while (have_posts()) : the_post();?>
    
    <h3 id="post-<?php the_ID(); ?>">
    <?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    
    <?php else : ?>
    <h3 class="center">No se han encontrado entradas</h3>
    <p class="center"><?php _e("Lo siento, pero lo que estas buscando no esta aqui."); ?></p>
    <?php endif;
    
    //Reset Query
    wp_reset_query();
    
    ?>

    The problem is that the last 3 posts are displayed, but I need the last 3 without the last one.

    Please help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • you could use the ‘offset’ parameter with query_posts():
    http://codex.ww.wp.xz.cn/Function_Reference/query_posts#Offset_Parameter

    disadvantage: is you want to use paginatin at a later point, it won’t work together with ‘offset’.

    or you could get the post from the first loop and use one of the mothods to avoid duplicate posts;
    described in here:
    http://codex.ww.wp.xz.cn/The_Loop#Multiple_Loops_in_Action

    Thread Starter agusparadak

    (@agusparadak)

    Thanks for the reply.

    This is how my code looks like now:

    <?php $my_query = new WP_Query('category_name=Noticias&posts_per_page=3');
      	while ($my_query->have_posts()) : $my_query->the_post();
      	$do_not_duplicate[] = $post->ID?>
    
    	<h3 id="post-<?php the_ID(); ?>">
    	<?php the_title(); ?></h3>
    	<?php the_excerpt(); ?>
    
      	<?php endwhile; ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post();
     	if (in_array($post->ID, $do_not_duplicate)) continue;?> 
    
    	<h3 id="post-<?php the_ID(); ?>">
    	<?php the_title(); ?></h3>
    	<?php the_excerpt(); ?>
    
      	<?php endwhile; endif; ?>

    Sorry for my ignorance, but I still have the same problem.
    The 3 latest posts are showed, but the last one is the one I don’t want to show, I suppose is the one which is not going to be duplicated, but it still shows.

    The thing is, how can I make reference to the last post and tell it not to show up? Because I’m already showing it above with this code:

    <div id="noticia_info">
    		<h2 id="post-<?php the_ID(); ?>">
            <?php the_title(); ?></h2>
    		<p><a href="<?php the_permalink() ?>">Ver art&iacute;culo</a></p>
    	</div>

    I don’t know if I’m making myself understood, my English isn’t that good 🙁

    try to add the line $do_not_duplicate[] = $post->ID;

    into here:

    <div id="noticia_info">
    		<h2 id="post-<?php the_ID(); ?>">
            <?php the_title(); ?></h2>
    		<p><a href="<?php the_permalink() ?>">Ver art&iacute;culo</a></p>
    	</div>

    maybe in this way:

    <div id="noticia_info">
    <?php $do_not_duplicate[] = $post->ID; ?>
    		<h2 id="post-<?php the_ID(); ?>">
            <?php the_title(); ?></h2>
    		<p><a href="<?php the_permalink() ?>">Ver art&iacute;culo</a></p>
    	</div>

    and then, for your ‘other news’ section, try to use this code:

    <?php
    
    query_posts(array ('posts_per_page' => 3, 'post__not_in' => $do_not_duplicate) ); 
    
    if (have_posts()) :
    while (have_posts()) : the_post();?>
    
    <h3 id="post-<?php the_ID(); ?>">
    <?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    
    <?php else : ?>
    <h3 class="center">No se han encontrado entradas</h3>
    <p class="center"><?php _e("Lo siento, pero lo que estas buscando no esta aqui."); ?></p>
    <?php endif;
    
    //Reset Query
    wp_reset_query();
    
    ?>
    Thread Starter agusparadak

    (@agusparadak)

    It worked!
    Thank you so much.

    you are welcome.

    well done,
    multiple loops can be intimidating at times – but you mastered them 😉

    (you could tick this thread off as ‘resolved’ – there is a checkbox or dropdown somewhere)

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to show specific posts in the loop (3 posts)’ is closed to new replies.