Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to put a widget as "first post" if it's active.. and let the posts continue after that.. It works, but the first post disappears and the second post becomes the first in line after the widget.. Can't see what i'm doing wrong..

I already have a widget that shows in between the posts as you can see.

<?php if (have_posts()) : ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php $i=1;?>
<?php while (have_posts()) : the_post(); ?>          

<?php if ( is_active_sidebar('post_widget_area') && $i==1 ) { ?>

<?php dynamic_sidebar('post_widget_area'); ?>

<? } else { ?> 

    <?php the_title(); ?><br>
    <?php the_excerpt(); ?>

    <?php } if($i==3): ?>
    <?php get_sidebar('bottom'); ?>

    <?php endif; ?>
    <?php $i++; ?>
    <?php endwhile; ?>
    <?php if($i<4): ?>
    <?php get_sidebar('bottom'); ?>

  <?php endif; ?>
<?php endif; ?>
share|improve this question
add comment

2 Answers

Think about what is happening.

You run the_post(); which sets $post to the first post in the Loop, but you don't use that post if your sidebar is active. Then the Loop continues and $post gets set to the second post in the Loop. You've lost your first post.

I don't see why that first sidebar needs to be inside the Loop. You only need to be inside the have_posts part to run the code when "the loop is active". Something like this is what it seems you are going for:

if (have_posts()) { 
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
  $i=1;

  if ( is_active_sidebar('post_widget_area') && $i==1 ) { 
    dynamic_sidebar('post_widget_area'); 
  }

  while (have_posts()) { 
    the_post();            
    the_title();
    the_excerpt(); 

    if($i==3) {
      get_sidebar('bottom'); 
    }
    $i++; 
  } 

  if($i<4) {
    get_sidebar('bottom'); 
  } 

}

Sorry. I had to edit that code into something readable just to tell what was happening at all.

While I did not make this change in the code, you don't need $i. $wp_query->current_post has the same information already, though it starts with 0 not 1.

share|improve this answer
    
I've just now end to edit the code to make it readable. And now I'm see your answer... @s_ha_dum –  G. M. Aug 20 '13 at 22:18
2  
Damn it, I have to type faster. –  vancoder Aug 20 '13 at 22:19
add comment

You have an else after your initial sidebar, so that iteration of the loop never gets to display the first post.

I've reformatted your code for legibility. Constantly opening and closing php tags is pointless, short tags (<?) are the devil's work, and WordPress coding standards should be used. (Also, I hate endif syntax, but that's just me).

Untested!

<?php
if ( have_posts() ) {
    $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
    $i = 1;
    while ( have_posts() ) {
        the_post();
        if ( is_active_sidebar( 'post_widget_area' ) && $i == 1 ) {
            dynamic_sidebar( 'post_widget_area' );
        }
        the_title();
        echo '<br />';
        the_excerpt();
        if ( $i == 3 ) {
            get_sidebar( 'bottom' );
        }
        $i++;
    }
    if ( $i < 4 ) {
        get_sidebar( 'bottom' );
    }
}
?>
share|improve this answer
3  
"Constantly opening and closing php tags is pointless, short tags (<?) are the devil's work, and WordPress coding standards should be used. (Also, I hate endif syntax, but that's just me)." -- Here! Here! These things do nothing but make code unreadable, especially the "alternative" control structure syntax, for which I have no end of hatred. –  s_ha_dum Aug 20 '13 at 22:27
    
Thanks for cleaning up the code! That got the first post back.. Because I have a widget in the middle it messes up the post order. I got 3 posts - widget - 3 more posts. Now it becomes: widget - 3 posts - widget - 3 posts. When the first widget is active only two post should show after it.. so one more if has to get in there. –  DennisT Aug 20 '13 at 22:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.