Liam
Liam

Reputation: 9855

While loop add class to first result

I have the following

while ( $query->have_posts() )
{
$query->the_post();
if ( $keys = get_post_custom_keys() )
{
    echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><div class='top-dets'><span class='card-title'>";
    echo the_title();
    echo "</span>";


// Network query 
    $network_value = get_post_custom_values('srchnetwork');
    foreach ( $network_value as $key => $value ) {
    echo '<span class="srch-val-">'. $value . '</span>'; }

// Pricing Query
    $pricing_value = get_post_custom_values('srchpricing');
    foreach ( $pricing_value as $key => $value ) {
    echo '<span class="srch-val-1">'. $value . '</span>'; }

// Setup Query
    $setup_value = get_post_custom_values('srchsetupfee');
    foreach ( $setup_value as $key => $value ) {
    echo '<span class="srch-val-2">'. $value . '</span>'; }

// Services Query
    $services_value = get_post_custom_values('srchservices');
    foreach ( $services_value as $key => $value ) {
    echo '<span class="srch-val-3">'. $value . '</span></div>'; }

// Big Card Query
    $bigcard_value = get_post_custom_values('bigcard');
    foreach ( $bigcard_value as $key => $value ) {
    echo '<img src="wp-content/themes/cafc/images/cards/'. $value . '" />'; }


//      echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';

    echo the_excerpt()."</div>";   }};
} 

Im wondering if its possible that I can wrap the first returned result within a span tag? if so how would I go about doing this? thanks

Upvotes: 0

Views: 3769

Answers (4)

Pankaj Yadav
Pankaj Yadav

Reputation: 37

You can take help from a counter :

$count  = 0;
while(){
  <div class="<?php $count++; if($count == 1) { echo ' active'; } ?>">hello</div>
}

Upvotes: 1

scottevans93
scottevans93

Reputation: 1079

My assumption on that basis would then result in something like this around each loop...

// Network query
$neti = 1;
$network_value = get_post_custom_values ( 'srchnetwork' );
foreach ( $network_value as $key => $value ) {
  if($neti == 1){
      echo '<span class="srch-val-">' . $value . '</span>';
  }else{
      echo $value;
  }
  $neti++;
}

Upvotes: 0

Jazz
Jazz

Reputation: 1485

As a general approach, any time you need behavior to only execute on the first time through a loop, you can simply use a flag variable to check whether to execute that behavior.

$firstLoop = true;
while( $query->have_posts() ){

    //do some things

    if( $firstLoop ){
        //do things on only the first loop
    }

    //do other things

    $firstLoop = false;
}

Or, for a foreach loop:

$firstLoop = true;
foreach( $network_value as $key => $value ){

    if( $firstLoop ){
        //do things on only the first loop
    }

    //do other things

    $firstLoop = false;
}

This avoids needing to count loops, and it works for any loop structure. Just remember to always set your flag variable to false at the end of the loop.

Upvotes: 3

safarov
safarov

Reputation: 7804

$i = -1;
while ( $query->have_posts() )
{
$i++;
$query->the_post();
if ( $keys = get_post_custom_keys() )
{
echo "<div class='clearfix card-prod ".($i?'first':'')."'><div class='top-dets'><span class='card-title'>";
echo the_title();
echo "</span>";
...

Check this line ( $i ? 'first' : '').

Upvotes: 2

Related Questions