dongshu7162 2014-05-07 21:09
浏览 17
已采纳

抓取header.php的页面名称

For my header.php file, I would like to grab the <title> via php and having trouble doing this. I know calling the constant would work, but that would mean I would need multiple files (which is bad practice). Any further help would be appreciated.

define("HOME", dirname(__DIR__)."index.php"); 
define("ABOUT", dirname(__DIR__)."../about/index.php"); 
define("WORK", dirname(__DIR__)."../work/index.php"); 
define("SERVICES", dirname(__DIR__)."../services/index.php"); 
define("CLIENTS", dirname(__DIR__)."../clients/index.php"); 
define("CONTACT", dirname(__DIR__)."../contact/index.php"); 

$page_titles = array (
    'home_page' => 'Home | Page',
    'about_page' => 'About | Page',
    'work_page' => 'Work | Page',
    'services_page' => 'Services | Page',
    'clients_page' => 'Clients | Page',
    'contact_page' => 'Contact | Page'
);

if (HOME == $page_titles[0]) {
    return $page_titles[0];
}

if (ABOUT == $page_titles[1]) {
    return $page_titles[1];
}

if (WORK == $page_titles[2]) {
    return $page_titles[2];
}

if (SERVICES == $page_titles[3]) {
    return $page_titles[3];
}

if (CLIENTS == $page_titles[4]) {
    return $page_titles[4];
}

if (CONTACT == $page_titles[5]) {
    return $page_titles[5];
}
  • 写回答

2条回答 默认 最新

  • douhuan3420 2014-05-07 21:39
    关注

    Your page should define its own title. But, if you really want to do it this way, here's a possible solution:

    whatever_page_you_load.php:

    define("PAGE", "WHATEVERPAGE");
    
    include("header.php");
    

    header.php:

    $page_names = array(
      "HOME" => "Home Page",
      "WHATEVERPAGE" => "Whatever Page's Name";
    );
    
    $title = "Default Title";
    
    if(defined("PAGE") && !empty($page_names[PAGE])) {
      $title = $page_names[PAGE];
    }
    
    echo "<title>" . htmlentities($title) . "</title>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?