Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 717645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:26:12+00:00 2026-05-14T05:26:12+00:00

I have a php script that echoes a series of uploaded images and I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:26:13+00:00Added an answer on May 14, 2026 at 5:26 am

    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;
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that echoes a large JSON array. This is a
I have php script that creates a temporary watermark image for users that are
I have a PHP script that is sending a series of http requests to
I have this script set up that echoes all relevant users in a database
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);
I have a php script that backup my table in .sql format and I
I have a PHP script that uses CURL to fetch a remote page and
I have a php script that is run once a minute. The script runs
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a php script that check if the referrer has been cleared after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.