WordPress.org

Ready to get started?Download WordPress

Forums

Could someone help me with a logic (else) problem (2 posts)

  1. CrudOMatic
    Member
    Posted 4 years ago #

    I've never written PHP before, so I'm having a hard time figuring this out:

    I have written an index page that displays posts from new to old in a certain layout. I want the newest post to be stretched across the content area - (class "size1of1") and older posts are two halves of the content area (two smaller, beside each other - class "size1of2"). The last post in a line has the "lastUnit" class - odd numbered posts will have it (including number 1, and the last post, whether even or odd), along with the closing /div (has the class of "line")that contains them.

    The problem is, for some reason the else logic isn't getting triggered. The loop goes through for the number of posts for the page, but the else logic never gets done - it's like it is skipping the IF block after the first go.

    here is the code for my loop (Sorry, HTML included, so it can be understood):

    <?php
    			$p_loop_count = 1;
    			$p_post_count = $wp_query->post_count;
    			if (have_posts()) : ?>
    			<?php while (have_posts()) : the_post(); ?>
    			<?php if ($p_loop_count == 1) { /* FIRST post */?>
    				<div class="line">
    					<div class="unit size1of1 lastUnit thumbnote">
    						<p>
    							<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br />
    							<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link(); ?></small>
    						</p>
    
    						<?php the_excerpt(); ?>
    
    						<p>
    							<small>category: <?php the_category(' ') ?></small><br />
    							<small>tags: <?php the_tags('', ' ', ''); ?></small><br />
    							<small><?php edit_post_link('edit', '', ' | '); ?>  <?php comments_popup_link('no comments »', '1 comment »', '% comments »'); ?></small>
    						</p>
    					</div>
    				</div>
    			<?php } else { /* Is NOT the first post */ ?>
    				<?php if ($p_loop_count % 2) { /* odd */?>
    					<div class="unit size1of2 lastUnit thumbnote">
    						<p>
    							<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br />
    							<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link(); ?></small>
    						</p>
    
    						<?php the_excerpt(); ?>
    
    						<p>
    							<small>category: <?php the_category(' ') ?></small><br />
    							<small>tags: <?php the_tags('', ' ', ''); ?></small><br />
    							<small><?php edit_post_link('edit', '', ' | '); ?>  <?php comments_popup_link('no comments »', '1 comment »', '% comments »'); ?></small>
    						</p>
    					</div>
    				</div>
    				<?php } ?>
    				<?php if (!$p_loop_count % 2) { /* even */?>
    					<?php if ($p_loop_count == $p_post_count) { /* the last post */?>
    				<div class="line">
    					<div class="unit size1of2 lastUnit thumbnote">
    						<p>
    							<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br />
    							<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link(); ?></small>
    						</p>
    
    						<?php the_excerpt(); ?>
    
    						<p>
    							<small>category: <?php the_category(' ') ?></small><br />
    							<small>tags: <?php the_tags('', ' ', ''); ?></small><br />
    							<small><?php edit_post_link('edit', '', ' | '); ?>  <?php comments_popup_link('no comments »', '1 comment »', '% comments »'); ?></small>
    						</p>
    					</div>
    				</div>
    					<?php } else { /* NOT the last post */?>
    				<div class="line">
    					<div class="unit size1of2 thumbnote">
    						<p>
    							<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br />
    							<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link(); ?></small>
    						</p>
    
    						<?php the_excerpt(); ?>
    
    						<p>
    							<small>category: <?php the_category(' ') ?></small><br />
    							<small>tags: <?php the_tags('', ' ', ''); ?></small><br />
    							<small><?php edit_post_link('edit', '', ' | '); ?>  <?php comments_popup_link('no comments »', '1 comment »', '% comments »'); ?></small>
    						</p>
    					</div>
    					<?php } ?>
    				<?php } ?>
    			<?php } ?>
    			<?php $p_loop_count++;
    				endwhile; ?>
    
    			<div class="line">
    				<div id="post-nav" class="unit size1of1 lastUnit aligncenter">
    					<?php next_posts_link('&laquo; Older Entries') ?> &nbsp; <?php previous_posts_link('Newer Entries &raquo;') ?>
    				</div>
    			</div>
    
    			<?php else : ?>
    
    			<h2 class="aligncenter">Not Found</h2>
    			<p class="aligncenter">Sorry, but you are looking for something that isn't here.</p>
    				<?php get_search_form(); ?>
    			<?php endif; ?>

    Any help would be appreciated.

  2. satakest
    Member
    Posted 4 years ago #

    $wp_query->current_post

    is the index of the current post within the loop so it may be easier to do something like this. (I' used post_class() for the div classes, it is in WP 2.7 + )

    //start the loop
    <?php if (have_posts()) : while (have - blah blah blah ?>
    ...
    <?php
    $myDivClasses = array('unit');
    // if it's the first(zeroth) post, size1of1 class else size1of2 class
    $myDivClasses []= ($wp_query->current_post == 0) ? 'size1of1' : 'size1of2';
    
    // add lastUnit class to odd divs (including first)
    if ($wp_query->current_post % 2 == 0)
    	$myDivClasses []= 'lastUnit';
    
    // if this is the last post found it's a lastUnit
    if ($wp_query->post_count == $wp_query->current_post + 1)
    	$myDivClasses []= 'lastUnit';
    ?>
    <!-- Now do your div with it's post classes -->
    <div <?php post_class($myDivClasses); ?>>
    ...
    </div>
    <?php endwhile; endif; ?>

    the []= operator adds a value to an array
    current_post is zero based so add 1 to it for comparison

Topic Closed

This topic has been closed to new replies.

About this Topic