开发者

PHP - Use H1 Tags as Page Title

开发者 https://www.devze.com 2023-01-23 09:11 出处:网络
I just started with php. I have a pretty simple site with header and footer includes and the content in between.

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 CM开发者_如何学编程S'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 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>


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消