WordPress.org

Ready to get started?Download WordPress

Forums

Writing something in the loop after the first post (5 posts)

  1. MASSERRA
    Member
    Posted 1 year ago #

    Hello everyone!!
    I'm having this problem that is suposed to be really easy and I don't know why it's not working... I'm trying to involve all the posts after the first one in a div to be hidden and collapsable...

    <?php query_posts( 'showposts=5' );?>
    <?php $a = 0; ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php $a = $a++; ?>
    	<?php if ($a == 2) : ?>
    		<span class="collapseomatic colomat-visited" id="id6158" alt="show more news" title="show more news">+</span>
    		<span id="swap-id6158" style="display:none;">—</span>
    		<div id="target-id6158" class="collapseomatic_content" style="display: none; ">
    	<?php else: endif; ?>
    	<?php the_content(); ?>
    <?php endwhile; else: endif; ?>
    <?php if ($a > 2){
    	echo "</div>";
    } ?>

    For that I created a variable $a that is going to be equal to the number of the current post in the loop, and when the post is the nº 2 I make it write the button to hide and show the rest of the content and open the div that will hide the content...than, after all the loop has finished, I close the div.

    What is appearing on the page is the content as if there was no variable $a and if else to write the enclosure div.

    Can someone understand this? O.o
    Thanks in advance!

  2. dariocanata
    Member
    Posted 1 year ago #

    I don't!

  3. cykeltomte
    Member
    Posted 1 year ago #

    Instead of <?php $a = $a++; ?> try <?php $a++; ?>, not sure if it will fix your problem, but to my understanding the first $a is unnecessary, or maybe wrong even.

  4. MASSERRA
    Member
    Posted 1 year ago #

    Thanks for the reply! That's the same, like doing a count, attributing the $a the value of the $a plus 1... But I tried and no... that doesn't work... :/

  5. MASSERRA
    Member
    Posted 1 year ago #

    Ups...I was wrong..! My friend changed the template I was using on that page and when I tried the $a++ I thought it was not working... but the $a++ worked!! I thought it was the same as what I had... but now that it worked I just saw the error I was doing... $a++; is only the same as $a = $a + 1; like I sad in the last comment and, still, I didn't notice... :P

Topic Closed

This topic has been closed to new replies.

About this Topic