• Hello everyone! First of all sorry me for my bad English but isn’t my native language so I’ll do as best as possible to explain my doubt.

    I’ve a site running the last version of WordPress (4.4) and I want to display the post on the index.php page with two conditions.

    – The first post must have a different style than the rest. AND
    – Since that post I want to put a wrapper div after two posts.

    The graphical form is:

    | FIRST POST DIFFERENT STYLE |
    |       OPEN WRAPPER DIV     |
    | OTHER POST|   | OTHER POST |
    |      CLOSE WRAPPER DIV     |
    |       OPEN WRAPPER DIV     |
    | OTHER POST|   | OTHER POST |
    |      CLOSE WRAPPER DIV     |
    |       OPEN WRAPPER DIV     |
    | OTHER POST|   | OTHER POST |
    |      CLOSE WRAPPER DIV     |

    The code of my index.php page:

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0; ?>
    <?php query_posts($query_string . '&cat=1,2'); ?>
    <?php /* Start the Loop */ ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $c++;
    if( !$paged && $c == 1) :?>
    // FIRST POST STYLE HERE
    
    <?php else :?>
    <?php if( $wp_query->current_post%2 == 0 ) echo "\n".'<div class="wrap">open'."\n"; ?>
    <?php get_template_part( 'content', get_post_format() ); ?>
    <?php if( $wp_query->current_post%2 == 1 || $wp_query->current_post == $wp_query->post_count-1 ) echo 'close</div> <!--/.wrap-->'."\n"; ?>
    <?php endif;?>
    <?php endwhile; ?>

    The first post has a different style but the wrapper didn’t work. If anyone can help me will be great. Thanks very much in advance! =)

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Dot,

    If you use the external counter that you’re setting up ($c) instead of $wp_query->current_post, does that output the correct wrapper code?

    Thread Starter Dot22

    (@dot22)

    Hello jkessel28! Thanks for your reply. I tried this, but didn’t work:

    <?php else :?>
    <?php if( $c == 2 ) echo "\n".'<div class="wrap">open'."\n"; ?>
    <?php get_template_part( 'content', get_post_format() ); ?>
    <?php if( $c == 2 ) echo 'close</div> <!--/.wrap-->'."\n"; ?>
    <?php endif;?>
    <?php endwhile; ?>

    The problem with counter defined as $c is that the first post (with different style) counts as post 1, but I need to start the count since the second post.

    The graphical form:

    | FIRST POST DIFFERENT-UNIQUE STYLE (no wrap, no count) |
    
    |       OPEN WRAPPER DIV     |
    | POST 1  |         | POST 2 |
    |      CLOSE WRAPPER DIV     |
    |       OPEN WRAPPER DIV     |
    | POST 3  |         | POST 4 |
    |      CLOSE WRAPPER DIV     |
    |       OPEN WRAPPER DIV     |
    | POST 5  |         | POST 6 |
    |      CLOSE WRAPPER DIV     |

    Any help will be appreciated! =)

    i have tried this code creating a template and it seems to be correct as per your demands, lets give it a try

    <?php
    /*
    * Template Name: Test Template
    */
    
    get_header();
    ?>
    <div id="primary" class="site-content">
    		<div id="content" role="main">
    
    			<?php
    
    				$args = array(
    				'post_type'	=> 'post',
    				'posts_per_page'	=> -1,
    				'post_status'	=> 'publish'
    				);
    			$query = new WP_Query($args);
    
    			if($query->have_posts()):
    
    				$i=0;
    
    				$count = $query->found_posts;
    
    				while($query->have_posts()):$query->the_post();
    
    				if($i == 0){
    
    					echo '<h3>'.get_the_title().'</h3>';
    
    					if($i != ($count-1)){
    
    					echo '<div class="inner-container">';
    
    					}
    					$i++;
    
    					continue;
    
    				}
    
    				echo '<h3>'.get_the_title().'</h3>';
    
    				if($i%2 == 0){
    					if($i != ($count-1)){
    					echo '</div>';
    					echo '<div class="inner-container">';
    				}
    				}
    
    				$i++;
    
    				endwhile;
    
    			endif;
    
    			?>
    
    		</div>
    	</div>
    <?php
    get_footer();
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘First post with different style and wrap every two posts’ is closed to new replies.