SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist mumford1's Avatar
    Join Date
    Sep 2002
    Location
    UK
    Posts
    572
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic PHP Title Based On Page Name

    Hi

    I have searched for help on automatically adding the page tile of my pages based on the page name itself.

    For Example:

    Site Name | Page Name

    So this page: www.sitename.com/about-us.php would return in the <title> tag:

    My Site | About Us

    I just cant find the right solution. Could somebody please help me on this?

    Many Thanks

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Use a combination of str_replace() and ucwords() functions to turn about-us.php into "About Us", but I dont see how you'd turn sitename.com to "My Site"

  3. #3
    SitePoint Evangelist mumford1's Avatar
    Join Date
    Sep 2002
    Location
    UK
    Posts
    572
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for the response, when I mentioned site name I meant for example "Sitepoint" or the name of the actual website.

    I am a complete novice to PHP so I am really unsure how I would get this to work.

    Thanks

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:
    PHP Code:
    <?php
    $page_title 
    'SiteName | ' ucwords(str_replace(array('-''/''.php'), array(' '), $_SERVER['PHP_SELF']));
    ?>
    <html>
    <head>
        <title><?= $page_title ?></title>
    </head>
    <body>

        <!-- whatever else you wanna put on your page -->
        
    </body>
    </html>

  5. #5
    SitePoint Evangelist mumford1's Avatar
    Join Date
    Sep 2002
    Location
    UK
    Posts
    572
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many Thanks for the reply.

    I tried your code but it is outputting the following


    <title>SiteName | Kingswoodprogrammesictsoundscape</title>

    The reason being I think is because some of my pages are in directories for example "Programmes"

    So I need it to be the following <title>SiteName | Programmes | ictsoundscape</title>

    Any ideas as to how I would do this?

    Thanks

  6. #6
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I gave you a good example. Figure out the rest.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You had better post some example strings showing what they are at the moment vs what you want them to read, and maybe explain where are you getting the rest of the page content from and why you only want the title of the page to be dynamic. Why not the <h1></h1> heading too?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    function fixTitle(&$input)
    {
        
    $input str_replace('-'' ',$input);
        
    $pos strpos($input,'.');
        if(
    $pos 0)
            
    $input substr($input,0,$pos);
        
    $input ucwords($input);
    }

    $aTitle explode('/',trim($_SERVER['REQUEST_URI'],'/'));
    $aTitle array_reverse($aTitle);
    array_walk($aTitlefixTitle);
    echo 
    implode(' | ',$aTitle).' | PUT SITENAME HERE';

    ?>
    For an url like: http://localhost/folder1/folder2/folder3/about-us.php it will output:

    About Us | Folder3 | Folder2 | Folder1 | PUT SITENAME HERE
    Maybe this can inspire you to create something nice.

  9. #9
    SitePoint Member blogaddition's Avatar
    Join Date
    Sep 2012
    Location
    India
    Posts
    23
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use The following code

    <?php
    $page = $_SERVER['REQUEST_URI'];
    $pageName = str_replace('/','',$page);
    $pageTitle = ucwords(str_replace('-',' ',$pageName));
    ?>
    <html>
    <head>
    <title><?php echo $pageTitle;?></title>
    </head>
    <body>
    <!-- Place you body contetn here-->
    </body>
    </html>


Bookmarks

Posting Permissions

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