简体   繁体   中英

PHP - how to change title of the page AFTER including header.php?

page.php:

<?php
include("header.php");
$title = "TITLE";
?>

header.php:

<title><?php echo $title; ?></title>

i want my title to be set AFTER including the header file. Is it possible to do this? I've seen some similiar stuff on php-fusion tvs: http://docs.php-fusion.it/temi:output_handling

expanding on Dainis Abols answer, and your question on output handling,

consider the following:

your header.php has the title tag set to <title>%TITLE%</title> ; the "%" are important since hardly anyone types %TITLE% so u can use that for str_replace() later.

then, you can use output buffer like so

<?php
    ob_start();
    include("header.php");
    $buffer=ob_get_contents();
    ob_end_clean();

    $buffer=str_replace("%TITLE%","NEW TITLE",$buffer);
    echo $buffer;
?>

and that should do it.

EDIT

I believe Guy's idea works better since it gives you a default if you need it, IE:

  • The title is now <title>Backup Title</title>
  • Code is now:
<?php
    ob_start();
    include("header.php");
    $buffer=ob_get_contents();
    ob_end_clean();

    $title = "page title";
    $buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '$1' . $title . '$3', $buffer);

    echo $buffer;
?>

1. Simply add $title variable before require function

<?php

$title = "Your title goes here";
require("header.php");

?>

2. Add following code into header.php

<title><?php echo $title; ?></title>

What you can do is, you store the output in a variable like:

header.php

<?php
   $output = '<html><title>%TITLE%</title><body>';
?>

PS: You need to remove all echos/prints etc so that all possible output is stored in the $output variable.

This can be easely done, by defining $output = ''; at the start of the file and then find/replace echo to $output .= .

And then replace the %TITLE% to what you need:

<?php
    include("header.php");
    $title = "TITLE";

    $output = str_replace('%TITLE%', $title, $output);
    echo $output;
?>

Another way is using javascript in your code, instead of:

<title><?php echo $title; ?></title>

Put this in there:

<script type="text/javascript">
    document.title = "<?=$title;?>"
</script>

Or jQuery, if you prefer:

<script type="text/javascript">
    $(document).ready(function() {
        $(this).attr("title", "<?=$title;?>");
    });
</script>

Expanding a little on we.mamat's answer, you could use a preg_replace instead of the simple replace and remove the need for a %title% altogether. Something like this:

<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();

$title = "page title";
$buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '$1' . $title . '$3', $buffer);

echo $buffer;
?>

you can set using JavaScript

<script language="javascript">
    document.title = "The new title goes here.";
</script>

Add this code on top your page

<?php
    $title="This is the new page title";
?>

Add this code on your Template header file (include)

<title><?php echo $title; ?></title>

It's very easy. Put this code in header.php

        <? 
$sitename = 'Your Site Name'
$pagetitle;

if(isset($pagetitle)){
 echo "<title>$pagetitle." | ". $sitename</title>";
} 
  else {
echo "<title>$sitename</title>";
}
?>

Then in the page put there :

 <?
    $pagetitle = 'Sign up'
    include "header.php";
    ?>

So if you are on Index.php , The title is Your Site Name. And for example if you are on sign up page , The title is Sign up | Your Site Name

Every Simple just using a function , I created it .

   <?
function change_meta_tags($title,$description,$keywords){
        // This function made by Jamil Hammash
    $output = ob_get_contents();
    if ( ob_get_length() > 0) { ob_end_clean(); }
    $patterns = array("/<title>(.*?)<\/title>/","<meta name='description' content='(.*)'>","<meta name='keywords' content='(.*)'>");
    $replacements = array("<title>$title</title>","meta name='description' content='$description'","meta name='keywords' content='$keywords'");

    $output = preg_replace($patterns, $replacements,$output);  
    echo $output;
}
?>

First of all you must create function.php file and put this function inside ,then make require under the MetaTags in Header.php . To use this function change_meta_tags("NEW TITLE","NEW DESCRIPTION",NEW KEYWORDS); . Don't use this function in Header.php !! just with another pages .

使用像这样的jQuery函数:

$("title").html('your title');

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