I'm trying to figure out how to write a code to display the image only once inside the loop.
For example in Wordpress
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
inside that loop I just want to place one image on the first post only.
I only know how to do that in C and C++ but I don't know how is that work on PHP?
Here is what I did to achieve this. Thanks to @Jack.
First I declare a variable before my Wordpress post loop start.
<?php
$displayed = false;
?>
Then I have the code that @Jack suggest with a small modification.
<?php
$image = 'your image URL';
if (!$displayed)
{
echo $image;
$displayed = true;
}
?>
And that's it now I can have my picture show only once!
Put a conditional inside the loop.
$displayed = false;
while ($someVariable)
{
if (!$displayed)
{
echo $image;
$displayed = true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.