• I am trying to extract the excerpt, title, permalink, etc from pages that I have created. It’s a two column setup, so I am in need of throwing in the class to every second div to clear the margin-right properties (for proper viewing). This is the code that I have so far:

    <?php if ( is_page() ) {  ?>
    	<?php
    	$args = array( 'post_parent' => 5, 'post_type'=> page, 'orderby'=> menu_order, 'numberposts'=> 10, 'order'=> ASC );
    	$myposts = get_posts( $args );
    	foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<?php static $count1 = 0; ?>
    	<div class="pdtx <?php if($count1%2) { echo 'last'; } ?>">
    	<h2><em><?php the_title(); ?></em></h2>
    	<p><?php $excerpt = get_post_meta($post->ID, 'excerpt', true); ?>
    	<?php echo $excerpt; ?></p>
    	</div>
    	<?php endforeach; ?>
    	<?php } ?>

    Anyone have an idea

Viewing 8 replies - 1 through 8 (of 8 total)
  • try to increment the counter before the line `<?php endforeach; ?>
    `

    example:
    <?php $count1++; ?>

    Thread Starter anthonyabraira

    (@anthonyabraira)

    brilliant! worked perfectly…

    Thread Starter anthonyabraira

    (@anthonyabraira)

    a bit of something is off here.. apparently it doesn’t know (second grouping of two column services @ https://twingravity.thruhere.net:8888/ashevillenerd_com/) that I need to reset the counter after each section is done.

    Thread Starter anthonyabraira

    (@anthonyabraira)

    never mind i figured it out. gotta assign each count its own variable:

    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php static $count2 = 0; ?>
    <div class="pdtx <?php if($count2%2) { echo 'last'; } ?>">
    Thread Starter anthonyabraira

    (@anthonyabraira)

    I thought I was out of the woods but not quite. I am pulling my hair out over this seemingly simple piece of code. This is what I have for a three column setup:

    <div class="c3 closing">
    <?php if ( is_page() ) {  ?>
    <?php // how this works section on footer of page describing service perk
    $args = array( 'post_parent' => 114, 'post_type'=> page, 'orderby'=> menu_order, 'numberposts'=> 10, 'order'=> ASC );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php static $count4 = 0; ?>
    <div <?php if($count4%3) { echo 'class="last"'; } ?>>
    <h2><strong><?php the_title(); ?></strong></h2>
    <p><?php $excerpt = get_post_meta($post->ID, 'excerpt', true); ?>
    <?php echo $excerpt; ?></p>
    </div>
    <?php $count4++; ?>
    <?php endforeach; ?>
    <?php } ?>
    </div><!-- c3 //-->

    This generates this HTML:

    <div class="c3 closing">
    <div >
    <h2><strong>Free Pick Up / Drop Off</strong></h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquet dui pulvinar nisl hendrerit eu accumsan massa porta. Mauris luctus mattis nisi. Cras volutpat viverra ultricies.</p>
    </div>
    <div class="last">
    <h2><strong>72 Hour Turnaround</strong></h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquet dui pulvinar nisl hendrerit eu accumsan massa porta. Mauris luctus mattis nisi. Cras volutpat viverra ultricies.</p>
    </div>
    <div class="last">
    <h2><strong>Print These Forms</strong></h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquet dui pulvinar nisl hendrerit eu accumsan massa porta. Mauris luctus mattis nisi. Cras volutpat viverra ultricies.</p>
    </div>
    </div><!-- c3 //-->

    What am I missing here. I want a way to tell it that every 3rd div, it should add the last class.

    Thanks in advance for the help so far.

    maybe try and change this:

    <?php static $count4 = 0; ?>
    <div <?php if($count4%3) { echo 'class="last"'; } ?>>

    to:

    <?php static $count4 = 1; ?>
    <div <?php if($count4%3==0) { echo 'class="last"'; } ?>>

    (the code with two columns only worked by chance)

    Thread Starter anthonyabraira

    (@anthonyabraira)

    Wonderful! You are a maestro at this. I would love to understand what it is you did here… cause I am having one pending weird bug on IE7 with the two column setup. If you have IE8 you can change it to IE7 mode and see what I mean.

    Only on the first section of the two column setup, do you see that the third row has a freak column flushed to the right rather than left. It continues a column structure at a different point:

    Check it out here: https://twingravity.thruhere.net:8888/ashevillenerd_com

    This is the code:

    <div class="c2" style="margin-top:20px;"><!-- Basic //-->
    <h1 class="basic hr"><span>Basic Repairs and Diagnostics</span></h1>
    <?php if ( is_page() ) {  ?>
    <?php
    $args = array( 'post_parent' => 5, 'post_type'=> page, 'orderby'=> menu_order, 'numberposts'=> 10, 'order'=> ASC );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php static $count1 = 0; ?>
    <div <?php if($count1%2) { echo 'class="last"'; } ?>>
    <?php $pu_price = get_post_meta( $post->ID, 'pu_price', $single = true ); // grab price custom field ?>
    <h2><em><?php the_title(); ?></em><span class="price">($<?php echo $pu_price ?>)</span></h2>
    <p><?php $excerpt = get_post_meta($post->ID, 'excerpt', true); ?>
    <?php echo $excerpt; ?></p>
    </div>
    <?php $count1++; ?>
    <?php endforeach; ?>
    <?php } ?>
    </div><!-- c2 //-->
    
    <div class="c2"><!-- Advanced //-->
    <h1 class="advanced hr"><span>Advanced PC Windows and Mac Upgrades and Hardware Installations</span></h1>
    
    <?php if ( is_page() ) {  ?>
    <?php
    $args = array( 'post_parent' => 63, 'post_type'=> page, 'orderby'=> menu_order, 'numberposts'=> 10, 'order'=> ASC );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php static $count2 = 0; ?>
    <div <?php if($count2%2) { echo 'class="last"'; } ?>>
    <?php $pu_price = get_post_meta( $post->ID, 'pu_price', $single = true ); // grab price custom field ?>
    <h2><em><?php the_title(); ?></em><span class="price">($<?php echo $pu_price ?>)</span></h2>
    <p><?php $excerpt = get_post_meta($post->ID, 'excerpt', true); ?>
    <?php echo $excerpt; ?></p>
    </div>
    <?php $count2++; ?>
    <?php endforeach; ?>
    <?php } ?>
    </div><!-- c2 //-->

    Thanks for walking me out almost completely out of the code “woods.”

    Thread Starter anthonyabraira

    (@anthonyabraira)

    At first I thought it had something to do with having a missing </div> in there somewhere. But I just validated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Adding "last" class to div’ is closed to new replies.