Bainn
Bainn

Reputation: 95

PHP: Show/Hide Div On Specific Pages

I've added the following code to pages to display a notice I only have to write once.

PHP

<?php require $_SERVER['DOCUMENT_ROOT']."/notice.php"; ?>

HTML

<div class="notice-1">
   <div class="date-event">2018-09-30 11:00</div> <!-- Hide notice when date/time expires -->
   <p class="notice-22-1">Daylight Saving begins Sunday 30th Sept
   </p>
</div>

From time to time, however, I want to display this message on particular pages, such as /contact.

Here's what I've come up with. I'm new to this, so I have no idea...

PHP

<?php 
    if( is_page( array( "new1", "contact1" ) ) ) {
        $showdiv = 'notice-1';
    }
?>

Note: I've assigned a page id (e.g. "contact1") to help make an array.

In action: https://www.citychurchchristchurch.co.nz/new

I can't get this to work. Any help you can provide would be greatly appreciated. Thanks.

Upvotes: 0

Views: 393

Answers (1)

Bobby Axe
Bobby Axe

Reputation: 1525

 <?php
      //list your intended pages in an array
      $intended_pages = array('page1', 'page7', 'page12');

      //get the current page name
      $page_name= basename($_SERVER['PHP_SELF']);

      //check if the current page is on the list
      If(in_array($page_name, $intended_pages))
      { 
        //current page is on the list so perform function
        $showdiv = 'notice-1'; 
      }
 ?>

Upvotes: 2

Related Questions