Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic page title using PHP

Tags:

php

dynamic

title

Hi I was wondering if anyone can help with this PHP problem.

Is it possible to use the text in a H2 tag and use that to populate the page title dynamically.

I'd also like to be able to use this same technique to add the H2 text into the meta description - Can anyone help?

like image 560
Stephen Meehan Avatar asked Jan 22 '23 16:01

Stephen Meehan


2 Answers

If your h2 text is dynamically made then other parts of your web page can be dynamically created too. For example, if you have a variable $a = "My Text"

<?php
$a = "My Text"
?>
<html>
<head>
<title><?php echo $a;?></title>
</head>
<body>
<h2><?php echo $a;?></h2>
</body>
</html>

Using this technique, you can define the text of other parts of the web page.

like image 116
scubacoder Avatar answered Jan 24 '23 05:01

scubacoder


That sounds like something that jQuery would excel at:

<script type='text/javascript' src='jquery-1.4-min.js'></script>
<script type="text/javascript">
    $(document).ready(function() {
        document.title = $('h2:first').text();
    });
</script>

To modify the meta data, you would do more of the same. I strongly recommend jQuery - Novice to Ninja as an amazing way to get into great depth with jQuery.

<html>
<head>
<meta description="" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('meta:first').attr('description', $('h2:first').text());
        alert($('meta:first').attr('description'));
    });
</script>
</head>
<body>
<h2>Testing 123</h2>
</body>
</html>
like image 37
Jonathan Avatar answered Jan 24 '23 07:01

Jonathan