Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Include w/ Custom Meta Tags?

    Hi. I have a template that I use that calls a common header to all pages through a PHP include.

    The problem is that I'd like to add custom meta tags individually to each page... but the template is calling the common header...

    Each page will need unique meta tags so I need some sort of hack that lets me input the meta tags directly into the HTML while still pulling the common header.

    Is this possible? Anyone have any tips?

    thanks in advance.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    pull them out of a DB depending on which page it is?

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If it's only a few pages use something like this:
    PHP Code:
    switch(basename($_SERVER['SCRIPT_NAME']))
    {
        case 
    'test.php':
            echo 
    '<meta...>';
        break;
        
        case 
    'contact.php':
            echo 
    '<other meta..>';
        break;
        
        default:
            echo 
    '<default meta...>';
        break;


  • #4
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the responses... you know i figured it out though. It's not the cleanest way to do it i'm sure but it works...

    If you're interested here's what I did.

    In the header include file I put:

    <meta name="description" content="<? if (isset($metadescription)) { echo $metadescription; } ?>">

    Then on the individual page I put this immediately before I called the include:

    <?php
    $subtitle = "Page Title Here";
    $metadescription = "Description Here";
    $metakeywords = "Keywords Here";
    require '../inc/header.inc.php'; ?>


    When I load the page and view source it all comes out as clean HTML with the meta tags properly coded. So the search engines should be fine w/ it.

    The subtitle is for the page title so each page can have a unique page title. The code in the include file is the same just w/ subtitle:

    <title><? if (isset($subtitle)) { echo $subtitle; } ?></title>



    As i said not the cleanest or easiest way i'm sure but it works. Hope someone else can use this.

    By the way anyone have any thoughts on how to better this?
    Last edited by phpnewbie99; 09-06-2007 at 01:39 AM.

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    INIGoesDR (lol)
    his idea is probably the better solution.
    I'd probably go with that.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    My solution is a little more complex, but in the end a lot cleaner and easier.

    I have a static class called UI that I create per site (it has some resuable components, so it can inherit a base class if need be). This class has several methods to add things like scripts/css/etc. But these can be called from specific scripts to add to the common header. It could be changed to set/modify specific meta tags.

    For example;

    home.php
    PHP Code:
        UI::addStylesheet('home'); 
    header.php
    PHP Code:
    // ... some basic html here ...

    foreach (UI::getStylesheets() as $css) {
        echo 
    '<link href="'.$css.'.css" rel="stylesheet" type="text/css" />';
    }

    // ... more basic html ... 
    This only works if your pages are executed before the header code. If you properly seperate model/views then you can add the stylesheet in the model, but that would be defeating the purpose of seperating business and presentation logic...

  • #7
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    If you're trying to create a small, easy to template based site, look into buffers and modrewrite...it's amazing what 50 lines of code can do.
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •