0

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;

I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.

<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
6
  • So the adverts depends on a url parameter to be set? Commented Nov 7, 2018 at 14:37
  • @Mike I tried this initially and it didn't work Commented Nov 7, 2018 at 14:40
  • why not <div class="advert" <?php if(empty($_GET['details'])) {echo "style='display: none'";} ?>></div> ? Commented Nov 7, 2018 at 14:44
  • 1
    it should be empty() instead of ! empty(), shouldn't it? Commented Nov 7, 2018 at 14:48
  • How do you determine the state for "do not display an ad"? Are you sure that you only want to check for a URL parameter? Commented Nov 7, 2018 at 15:48

2 Answers 2

1

Why not completely not echo "advert" element?

   if ( !empty($_GET['details']) ){
       echo '<div class="advert">add text</div>';
    }

if you really want to just hide, you can assign hide class

<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>

then you would need to add "hide" class with display:none in your style.css

Above is shorthand/ternary if/else statement used, its great if you need output some string.

And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.

Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().

Sign up to request clarification or add additional context in comments.

Comments

0

This should do it for you

<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
    $advert_display_string = "style='display: none'";
}
?>

<div class="advert" <?php echo $advert_display_string ; ?> ></div>`

but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below

<?php
if (isset($_GET['details'])) {
?>
    <div class="advert"></div>
<?
}
?>

Comments

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.