Reputation: 5099
I have a php script that echoes a series of uploaded images and I need to apply a class to the first image echoed in the series. Is this possible? For example, if I want to display 10 images and apply a class to only the first image, how would I go about it?
Here is the code, I am working with:
<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 "";
}
?>
On line 13, the code looks like this:
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
For the first item only, I need it to look like this:
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
EDIT:
So I have updated my code after reading everyones suggestions to look like this:
<?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 "";
}
?>
Then my browser crashed, so I suspect I didn't put this together correctly....Any suggestions?
Upvotes: 0
Views: 133
Reputation: 48887
Just add a boolean:
$firstTime = true;
while(have_posts())
{
if($firstTime)
{
$firstTime = false;
$class = 'class="show"';
}
else
{
$class = '';
}
}
Upvotes: 1
Reputation: 134275
Use a Loop Counter variable to count the iterations:
query_posts('cat=7');
$count = 0
while(have_posts())
Then, use it in your echo:
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' ";
}
Then at the bottom of your loop, make sure to increment it:
echo "";
$count = $count + 1;
}
?>
Upvotes: 2
Reputation: 1451
Add a counter
$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' ";
}
}
Upvotes: 1
Reputation: 12323
Just do:
$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++;
}
Upvotes: 1
Reputation: 31015
You might want to use a for loop instead of the while and add the class just during the first iteration.
Upvotes: 0
Reputation: 401182
You could use a counter, to determine on which iteration you are :
$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' ";
}
Upvotes: 1