Wordpress - Different html for posts within loop -
what i'm trying do:
write elseif statement in loop display different html markup first, second , third posts on page.
the code
<div id="content"> <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count == 1) : ?> <!-- 1st post --> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> //the html first post <?php the_title(); ?> //etc </article> <!-- end div post --> <?php elseif : ?> <?php if ($count == 2) : ?> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> //second post <?php the_title(); ?> //etc </article> <!-- end div post --> <?php elseif : ?> <?php if ($count == 3) : ?> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> //third post <?php the_title(); ?> //etc </article> <!-- end div post --> <?php else : ?> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> other posts <?php the_title(); ?> etc </article> <!-- end div post --> <?php endif; ?> <?php endwhile; ?> <?php endif; ?> </div> <!-- end div content -->
what's happening:
nothing. i'm doing wrong. i've commented code should have idea of i'm trying achieve here. perhaps can point me in right direction!
here's working code, first post has different markup others.
<div id="content"> <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count == 1) : ?> <!-- 1st post --> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> //the html first post <?php the_title(); ?> //etc </article> <!-- end div post --> <?php else : ?> <article <?php post_class() ?> id="post-<?php the_id(); ?>"> other posts <?php the_title(); ?> etc </article> <!-- end div post --> <?php endif; ?> <?php endwhile; ?> <?php endif; ?> </div> <!-- end div content -->
i want expand have different html first, second , third posts.
problem
<?php elseif : ?> <?php if ($count == 3) : ?>
it's should
elseif($count == 3) :
better way
you can use get_template_part reduce complexity nested if. created separate templates articlepart.php , articlepart-1.php etc.
if(have_posts()): $count = 0; while (have_posts()) : the_post(); $count++; get_template_part('articlepart', $count); endwhile; endif;
Comments
Post a Comment