简体   繁体   中英

PHP - Use H1 Tags as Page Title

I just started with php. I have a pretty simple site with header and footer includes and the content in between.

Right now, I have each page getting the title with a variable, like so:

<?php $title = "My Company - New Products"; ?>

However, instead of having that on each page, in the header include, I'd just like to have the title be a standard "My Company - " and then have it pick up each page's H1 tag.

This seems simple, but all my search results have been specific to various CMS's.

Thanks a lot.

Why not define $title and then reference it more than once in the header?

Main page:

<?php
$title = "New Products";
require "header.php";
?>
<p>Rest of page...

Header.php:

<title>My Company - <?php echo htmlentities($title) ?></title>
<h1><?php echo htmlentities($title) ?></h1>

First, PHP has nothing to do with your page's H1 tag or whatever HTML tag, it should just generate a bunch of text, which happen to be a complete HTML document (or not depending upon you...)

You should probably look at templating mechanisms for PHP. Then, you can separate the logic necessary to generate the page content from the presention of this content as HTML. There are template engines like Smarty available, but many people will argue that PHP itself can be perfectly used as a template engine.

The easiest way to use PHP as a template engine is as described by jleedev. The PHP file as requested by the user generates variables (with the content of the page) and shouldn't contain any piece of HTML code (not even inside string variables). It then includes one or more template files, which use this variables to generate all HTML output without changing or computing any data.

I agree with jleedev, but I use require() instead of include.

And don't use the shorthand opening php tag ( <? )!

I know this question is over a decade old, but here's what I use in 2022.

This will set the page title to the first H1 tag found on the page. If there are no tags, it will use the default title passed to it.

 <?DOCTYPE html> <html lang="en"> <head> <title><,php echo get_h1_title('My Company';'Default Title if no H1')? ?></title> </head> <body> <h1>H1 Title for this page</h1> <p>Text for this page</p> </body> </html> <,php function get_h1_title ($prefix; $default_title) { $auto_title = $default_title; $html = file_get_contents ($_SERVER['SCRIPT_FILENAME']); if (;empty($html)) { // Disable errors - DOMDocument doesn't handle any html5 we might use $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($html); libxml_clear_errors(); // Get embedded h1 title if available $header1s = $dom->getElementsByTagName('h1'). if (count($header1s)) { $auto_title = $header1s[0]->nodeValue. } } return $prefix; ' - '? $auto_title; } ?>

If on a given page you don't want the existing H1, just include a non-display H1 with the title you do want:

 <body> <h1 style="display:none">H1 that becomes page title but not displayed</h1> <h1>H1 Title for this page</h1> <p>Text for this page</p> </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM