1

I'm trying to setup my WordPress website so that if a post / page has a featured image assigned, this image will be used as the page banner. If however the page doesn't have a featured image, it must onload select a random image out of six available options. I've tried to used this if statement below:

<div id="slider">
    <div class="theslide">
        <?php
        if ( has_post_thumbnail() ) {
            the_post_thumbnail();
        }
        else {
            echo '<img src="/wp-content/uploads/link-ship-chandlers-banner-' . $random = rand(1,6); '.jpg">';
        }
        ?>
    </div>
</div>

It works, but the random number function is not closing preperly, so the code ends up looking like this:

<div id="slider">
<div class="theslide">
    <img src="/wp-content/uploads/link-ship-chandlers-banner-6  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Instead of like this:

<div id="slider">
<div class="theslide">
    <img src="/wp-content/uploads/link-ship-chandlers-banner-6.jpg">
</div>
</div>

I'm assuming my syntax is wrong for using php inside of the echo, but everything I try either has the same problem or cause a php error.

Any help would be appreciated.

Thanks in advance
Willem

1
  • As Mikk3lRo pointed out is his code only post. You should just use the rand(1,6). Also don't use semi-colon before the end of the echo. If you need the value of $random later in your code then assign it prior to the echo. Commented Jul 9, 2015 at 8:53

2 Answers 2

5

If you don't need to know which header was chosen, just do this:

echo '<img src="/wp-content/uploads/link-ship-chandlers-banner-' . rand(1,6) . '.jpg">';

Using more than one ; on a line is a no-go (you are essentially telling the php-interpreter that '.jpg">'; is an independant command - which will do nothing)

Assigning a variable (ie. $whatever = 'something) inside the echo statement won't be a problem in this case - though it really doesn't do any good either. What it does is create a new variable called $random that you could use afterwards to find out which header was used - but results will be unpredictable if used in the echo statement (ie. in your case the variable would contain [random number].jpg, not just the random number), instead assign the random number to $random first like this:

$random = rand(1,6);
echo '<img src="/wp-content/uploads/link-ship-chandlers-banner-' . $random . '.jpg">';
echo "We are using header {$random}, which was chosen at random.";

Note that the above example also shows an alternate way to include the variable in a string - using double-quotes you can simply write the variable directly inside the string itself. When doing so I recommend always using {} to wrap the variable though not needed in this case - this allows referencing more complex variables (such as array elements or object properties), and it makes the whole thing more readable too.

Other (IMO less readable, possibly more error-prone) solutions include:

$random = rand(1,6);
echo "<img src=\"/wp-content/uploads/link-ship-chandlers-banner-$random.jpg\">";
echo sprintf('<img src="/wp-content/uploads/link-ship-chandlers-banner-%d.jpg">', $random);
printf('<img src="/wp-content/uploads/link-ship-chandlers-banner-%d.jpg">', $random);
Sign up to request clarification or add additional context in comments.

Comments

1

Actually this can work, but you end the line with ;. So removing the ; and adding a .

echo '<img src="/wp-content/uploads/link-ship-chandlers-banner-' . $random = rand(1,6) . '.jpg">';

do this alternatively you don't need to set this variable

echo '<img src="/wp-content/uploads/link-ship-chandlers-banner-' . rand(1,6) . '.jpg">';

3 Comments

True it'll work. But I recommend setting the variable prior to the echo to make to code that tiny bit more readable. Or just skip assigning at all if it's not used again later.
@VuoriLiikaluoma i just said, it will work, not that this is good code (:
I know, I was just confirming that it does work and recommending otherwise for other readers. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.