WordPress.org

Ready to get started?Download WordPress

Forums

First and last post in loop (4 posts)

  1. Elvis
    Member
    Posted 1 year ago #

    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; ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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//-->
  3. Elvis
    Member
    Posted 1 year ago #

    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; ?>
  4. Elvis
    Member
    Posted 1 year ago #

    All okey, I made mistake! Thanks! :)

Topic Closed

This topic has been closed to new replies.

About this Topic