使用PHP将特定类应用于系列中的第一项

时间:2010-04-02 15:58:04

标签: php html

我有一个PHP脚本,它回应了一系列上传的图像,我需要将一个类应用于系列中回显的第一个图像。这可能吗?例如,如果我想显示10个图像并仅将类应用于第一个图像,我该怎么做呢?

以下是我正在使用的代码:

<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 "";
}
?>

在第13行,代码如下所示:

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

仅限第一项,我需要它看起来像这样:

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

编辑:

所以我在阅读了每个人的建议之后更新了我的代码:

<?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 "";
}
?>

然后我的浏览器崩溃了,所以我怀疑我没有把它正确地放在一起......有什么建议吗?

6 个答案:

答案 0 :(得分:2)

使用Loop Counter变量来计算迭代次数:

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

然后,在你的回声中使用它:

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' "; 
}

然后在循环的底部,确保增加它:

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

?>

答案 1 :(得分:1)

您可以使用计数器来确定您的迭代次数:

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

    $i_posts++;
}


这意味着您的第13行将被替换为以下内容:

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' ";
}

答案 2 :(得分:1)

只是做:

$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++;
}

答案 3 :(得分:1)

添加一个计数器

$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' ";
    }
}

答案 4 :(得分:1)

只需添加一个布尔值:

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

答案 5 :(得分:0)

您可能希望使用for循环而不是while,并在第一次迭代期间添加类。