PHP: meta tags in header.php

Discussion in 'PHP' started by canalboatman, Nov 3, 2008.

  1. #1
    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.
     
    canalboatman, Nov 3, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    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.
     
    shallowink, Nov 3, 2008 IP
  3. canalboatman

    canalboatman Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    canalboatman, Nov 4, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    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.
     
    shallowink, Nov 4, 2008 IP
  5. canalboatman

    canalboatman Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Not a shed load - about 25 pages in total.

    It's very kind of you.
     
    canalboatman, Nov 4, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    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.
     
    shallowink, Nov 4, 2008 IP
  7. canalboatman

    canalboatman Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    canalboatman, Nov 4, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    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.
     
    shallowink, Nov 4, 2008 IP
  9. canalboatman

    canalboatman Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    canalboatman, Nov 4, 2008 IP
  10. canalboatman

    canalboatman Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just to say thanks - you've been a great help and it works perfectly. I owe you!:)
     
    canalboatman, Nov 9, 2008 IP
  11. suehutton

    suehutton Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    suehutton, Jan 7, 2009 IP