How do I vary the meta description tag in header.php for the different pages that header is attached to? For example: Do I put <?php echo $desc; ?> in the header.php, and then in, say index.php a request such as: $desc = '<meta name="description" content="blah, blah, blah, etc" />' Or will that not work in which case how do I vary the meta description for each page? Advice much appreciated.
Is your site setup to use a database for pages or is it all based off includes? If it's using includes, you could define the meta before calling the header. $meta = "Page about Foo but not Bar" Then in header.php check if $meta is defined. If it is, use it else use a fallback.
Thanks. Mine is "includes", not stored in a database. I don't suppose you could tell me waht code to insert in the header, and what to insert in the page, e.g. index.php? so that the meta tag descrition changes according which page is displayed?
Sure, I'll help. How many pages are we talking about here? If its a large # of files, might be best to auto detect the page to centralize the upkeep.
Alright, here's the basics. At the start of header.php : <?php if(!isset($meta)) { $meta = "Default Meta Description"; } ?> HTML CODE <meta name="description" content="<?php echo $meta;?>" />' REST OF HTML CODE This is all that goes into header.php. So say you have page1.php <?php $meta = "This is all about page1"; include "header.php"; ?> That's pretty much it.
So to incorporate in my existing code in the header would I change it to: <title><?php echo $page_title; ?></title> <?php if(!isset($meta)) { $meta = "Default Meta Description"; } ?> HTML CODE <meta name="description" content="<?php echo $meta;?>" />' REST OF HTML CODE This is all that goes into header.php. So say you have page1.php <?php $page_title = 'Canal boats, narrowboats, tugs and Dutch barges for sale'; $meta = "This is all about page1"; include "header.php"; ?> Or is that not going to work?
It'll work. IF your site is about boats, change the Default Meta Description(the $meta defined in header.php) something like "Canal boats, narrowboats and Dutch Barges for sale" . This is what is used as the meta description if its not set in the page. Taking a tug boat for sale page as a specific example. In the tug_boat.php, $meta is defined like "Tug Boat for sale ", this is in that specific page. I probably just complicated it. So if you got questions, just ask.
That seems to make sense to me. I simply change your "This is Page 1" text. Once again, thanks for all your help, and enjoy the rest of the day wherever you are. Edited to add: Sorry, just saw you're in GA, which is Georgia I assume. I believe you have an interesting election going on right there at the moment. It'll be on TV over here in England all night.
I was about to fire off a message to say that I hadn't got this to work on my website when I realised I'd put a code snippet in the wrong place. And now it does. Thank you very much for such a helpful solution.