PHP ile bir dizi ilk öğe için belirli bir sınıf uygula

6 Cevap php

Ben yüklenen görüntülerin bir dizi yankıları bir php komut dosyası var ve ben serisinin yankılandı ilk görüntüye bir sınıf uygulamak gerekir. Bu mümkün mü? Ben 10 görüntüleri ve sadece ilk görüntü için bir sınıf uygulamak istiyorsanız Örneğin, onu nasıl hakkında gitmek istiyorsunuz?

İşte kod ben çalışıyorum, bir:

<div id="gallery">  
<?php
query_posts('cat=7');
while(have_posts())
{
the_post();
$image_tag = wp_get_post_image('return_html=true');
$resized_img = getphpthumburl($image_tag,'h=387&w=587&zc=1'); 
$url = get_permalink();
$Price ='Price'; 
$Location = 'Location';
$title = $post->post_title;
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
echo "rel=\"<div class='gallery_title'><h2>";
echo $title;
echo "</h2></div>";
echo "<div class='pre_box'>Rate:</div><div class='entry'>\$";
echo get_post_meta($post->ID, $Price, true);
echo "</div><div class='pre_box'>Location:</div><div class='entry'>";
echo get_post_meta($post->ID, $Location, true);
echo "</div>\"";
echo  "'/></a>";
echo "";
}
?>

Hat 13, kod şöyle görünür:

echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";

Sadece ilk öğe için, ben bu gibi bakmak gerekir:

echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";

EDIT:

Yani bu gibi bakmak herkesin önerileri okuduktan sonra kodumu güncelledik:

<?php
query_posts('cat=7');
while(have_posts())
{
the_post();
$image_tag = wp_get_post_image('return_html=true');
$resized_img = getphpthumburl($image_tag,'h=387&w=587&zc=1'); 
$url = get_permalink();
$counter = 0;
$Price ='Price'; 
$Location = 'Location';
$title = $post->post_title;
while(have_posts())
{
    $counter++;
    if ($counter > 1) {
        echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
    } else {
        echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
    }
}
echo "rel=\"<div class='gallery_title'><h2>";
echo $title;
echo "</h2></div>";
echo "<div class='pre_box'>Rate:</div><div class='entry'>\$";
echo get_post_meta($post->ID, $Price, true);
echo "</div><div class='pre_box'>Location:</div><div class='entry'>";
echo get_post_meta($post->ID, $Location, true);
echo "</div>\"";
echo  "'/></a>";
echo "";
}
?>

Sonra benim tarayıcı çöktü, bu yüzden ben .... birlikte doğru bir öneriniz bu koymadı şüpheli?

6 Cevap

Yineleme saymak için bir Loop Counter değişkeni kullanabilirsiniz:

query_posts('cat=7');
$count = 0
while(have_posts())

Sonra, yankı kullanabilirsiniz:

if ($count == 0){
   // echo with class
   echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
}
else {
   // echo without class
   echo "<a href='$url'><img src='$resized_img' width='587' height='387' "; 
}

Sonra döngü dibinde, bunu artırmak için emin olun:

  echo "";
  $count = $count + 1;
}

?>

Sen hangi yineleme üzerinde belirlemek için, bir sayaç kullanabilirsiniz:

$i_posts = 0;
query_posts('cat=7');
while(have_posts())
{
    if ($i_posts == 0) {
        echo "first iteration";
    }

    $i_posts++;
}


This means your line 13 would be replaced by something like this :

if ($i_posts == 0) {
    // First iteration of the loop : special class
    echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
} else {
    // Next iterations : no special class
    echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
}

Sadece yapın:

$c = 0;
query_posts('cat=7');
while(have_posts())
{   
*** snip ***
if ($c == 0) {
  $class = " class='show'";
} else {
  $class = "";
}
echo "<a href='$url'$class><img src='$resized_img' width='587' height='387' ";
*** snip ***
$c++;
}

Bir sayacı ekleme

$counter = 0;
while(have_posts())
{
    $counter++;
    if ($counter > 1) {
        echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
    } else {
        echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
    }
}

Sadece bir boolean ekleyin:

$firstTime = true;
while(have_posts())
{
    if($firstTime)
    {
        $firstTime = false;
        $class = 'class="show"';
    }
    else
    {
        $class = '';
    }
}    

Yerine while döngüsü için kullanmak ve sadece ilk yineleme sırasında sınıfı eklemek isteyebilirsiniz.