Support » Themes and Templates » First and last post in loop

  • Elvis

    (@elwins)


    Hi!
    I have code like this, there first post is diferent styled, like others, but how to style different last one?

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php query_posts( 'posts_per_page=8&cat=13' );?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $c++;
    if( !$paged && $c == 1) :?>
    
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <div class="post-entry">
                        <?php if ( has_post_thumbnail()) : ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                        <?php the_post_thumbnail(); ?>
                            </a>
                        <?php endif; ?>
    	<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    	<?php edit_post_link(__('<img src="' . site_url('') . '/wp-content/themes/metroons/icons/edit.png" />', 'metroons')); ?>
    	</h1>
    					<?php the_content(''); ?>
                    </div><!-- end of .post-entry -->
    
                </div><!-- end of #post-<?php the_ID(); ?> -->
    		<div class="post-titles">
    	    <?php else : ?>
    	<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    	<?php edit_post_link(__('<img src="' . site_url('') . '/wp-content/themes/metroons/icons/edit.png" />', 'metroons')); ?>
    	</h2>
    		<?php endif;?>
    
    		<?php endwhile; ?> 
    
    <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    for the last post per page, before this line <?php else : ?> add an ‘elseif’ section;

    <?php elseif($c == $wp_query->post_count) : ?>
    <!--//HOW TO SHOW LAST POST ON PAGE//-->

    ps:

    for the last post only on the last page:

    <?php elseif( ($wp_query->current_post+1 == $wp_query->post_count) && ($wp_query->max_num_pages == get_query_var('paged')) ) : ?>
    <!--//HOW TO SHOW LAST POST OF ALL POSTS//-->
    Elvis

    (@elwins)

    It works but last post is shown 2 times.

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php query_posts( 'posts_per_page=8&cat=13' );?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $c++;
    if( !$paged && $c == 1) :?>
    
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <div class="post-entry">
                        <?php if ( has_post_thumbnail()) : ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                        <?php the_post_thumbnail(); ?>
                            </a>
                        <?php endif; ?>
    	<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    	<?php edit_post_link(__('<img src="' . site_url('') . '/wp-content/themes/default/icons/edit.png" />', 'default')); ?>
    	</h1>
    					<?php the_content(''); ?>
                    </div><!-- end of .post-entry -->
    
                </div><!-- end of #post-<?php the_ID(); ?> -->
    		<div class="post-titles">
    
    	<?php elseif($c == $wp_query->post_count) : ?>
    
    	<h2 class="last"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    	<?php edit_post_link(__('<img src="' . site_url('') . '/wp-content/themes/default/icons/edit.png" />', 'default')); ?>
    	</h2>
    	    <?php else : ?>
    	<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    	<?php edit_post_link(__('<img src="' . site_url('') . '/wp-content/themes/default/icons/edit.png" />', 'default')); ?>
    	</h2>
    
    		<?php endif;?>
    
    		<?php endwhile; ?> 
    
    <?php endif; ?>

    Elvis

    (@elwins)

    All okey, I made mistake! Thanks! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘First and last post in loop’ is closed to new replies.