• Resolved madsrh123

    (@madsrh123)


    Is there a more efficient way to increase these numbers? I’ve looked into $i++ but couldn’t make it work.

    <?php $args = array( 'post_type' => 'album' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    
    	$track1 = get_field('track1');
    		if($track1 !== '') {
    		echo '<br><strong>1 <strong>'; the_field('track1'); } 
    
    	$track2 = get_field('track2');
    		if($track2 !== '') {
    		echo '<br><strong>2 </strong>'; the_field('track2'); } 
    
    	$track3 = get_field('track3');
    		if($track3 !== '') {
    		echo '<br><strong>3 </strong>'; the_field('track3'); }
    endwhile; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • What about something close to this (not tested..):

    $i = 1;
    
    while ($i > 0) {
        $track = get_field ('track'.$i);
    
        if ($track != '') {
            echo '<br><strong>1 <strong>'; the_field ('track'.$i);
            $i++'
        }
        else {
            $i = 0;
        }
    }

    There’s more efficient ways to do this, like getting all of the tracks in a function and using a foeraech() loop to go through them, but this is a starting point.

    Thread Starter madsrh123

    (@madsrh123)

    Thank you Catacaustic!
    This works perfectly. There was a typo at $i++’ and I changed the number inside to $i – exactly what I was looking for.
    You’ve been very helpful, thanks again ??

    while ($i > 0) {
        $track = get_field ('track'.$i);
    
        if ($track != '') {
            echo '<br><strong>' . $i . ' <strong>'; the_field ('track'.$i);
    	$i++;
        }
        else {
            $i = 0;
        }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to increase numbers if exist’ is closed to new replies.