首页 > 解决方案 > PHP:在特定页面上显示/隐藏 div

问题描述

我已将以下代码添加到页面以显示我只需编写一次的通知。

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>

但是,有时我想在特定页面上显示此消息,例如 /contact。

这就是我想出的。我是新手,所以我不知道...

PHP

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

注意:我已经分配了一个页面 ID(例如“contact1”)来帮助创建一个数组。

在行动:https ://www.citychurchchristchurch.co.nz/new

我不能让它工作。您能提供的任何帮助将不胜感激。谢谢。

标签: php

解决方案


 <?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'; 
      }
 ?>

推荐阅读