Force a page title rewrite

Asked

Viewed 96 times

1

Well, I have a page, but one of those pages wanted you to change the title. Example my page uses in Reader a code that defines the title of all pages, but I have a custom php page that also want to put a different title, ie Rewrite the title that is already defined in Reader, there is some way in PHP or any other to do this ?

<?php   
include_once "./includes/header.php";   
 <!-- AQUI VAI MEU CONTEÚDO -->  
include_once "./includes/footer.php"; 
?>

It would be like that, but I can’t edit header.php Then you’ll have to force the title change for what’s in the <!-- AQUI VAI MEU CONTEÚDO -->

  • You can post the code snippet. Because this should be done basically using a conditional structure.

  • Okay, I put an example in code!

  • You can change this file : "./includes/header.php"; ?

  • No, because a ready script Jah!

1 answer

2


You can do this using Javascript or jQuery, but it would be better if you chose to edit directly on ./includes/header.php.

Javascript:

document.title = 'Novo título';

jQuery:

$('title').text('Novo título');

Another solution, which I believe can also be done in a better way. It would be you at ./includes/header.php where you define the title pass a variable instead of a static text like:

Using tags short:

<title><?= $page_title ?? "Título estático" ?></title>

Using the traditional tags:

<title><?php echo $page_title ?? "Título estático"; ?></title>

In this example, if I’m not mistaken, it checks whether the variable $page_title exists using the function isset, if it exists, its value is passed, otherwise, "Static title".

And that way you would add the variable before the ./includes/header.php in that way:

<?php
// Define o título aqui.
$page_title = 'Título da página';

include_once "./includes/header.php";   
<!-- AQUI VAI MEU CONTEÚDO -->  
include_once "./includes/footer.php"; 
?>
  • How can I put this function in PHP $file->originalFilename inside the "New title" ?

  • @Matheusvitor If what you want is to add the text of $file->originalFilename for Javascript/Jquery can do so: <?= $file->originalFilename ?> (Replace "New title" with this excerpt), or if you want to do this with the PHP example just assign the value as you normally do: $page_title = $file->originalFilename;.

  • In my Header.php you have this code inside the title tag: <?php echo validation::safeOutputToScreen(PAGE_NAME); ?>

  • @Matheusvitor The constant PAGE_NAME is the one that defines the title? If it is you can use the PHP example: <?php echo validation::safeOutputToScreen($page_title ?? PAGE_NAME); ?> and then set before the header.php, probably you can also do this with the constant, but it would be less certain, because constants must be constant and immutable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.