SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Non-Member
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb PHP Code for "If Homepage then XXX, else"

    I am just editing the title tags for one of my sites, and I want it like (example):

    For the home page: Welcome to XXXX - The Best Site for XXXX

    For other pages: Welcome to XXXX

    Is there a PHP query or something that I can put in the <title> tags to check if it is the homepage or not?

    Thanks!

  2. #2
    SitePoint Wizard Rick's Avatar
    Join Date
    Oct 2002
    Location
    Lancashire, UK
    Posts
    3,847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming your homepage is called index.php this should work:

    PHP Code:
    <title>Welcome to example.org <?php echo ($_SERVER['PHP_SELF'] == '/index.php') ? ' - The Best Site for XXXX' ''?></title>
    Rick

  3. #3
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Maine
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I think he wants it different for every page.

    Rather than having a ton of if/then statements, just do something like this.

    PHP Code:
    <?php $title 'Home (this changes for every page)'?>
    <title><?php echo $title?> - Title Constant</title>


  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chatmasta View Post
    But I think he wants it different for every page.
    That's not how I read the question. I would have given much the same answer as did Rick.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    Non-Member
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there

    Thanks for the help, I was just after the first solution. The homepage only needs to be different, all the inner pages I can already vary.

    However, it appears my site won't parse the PHP in the title tags for some reason, I might post in the javascript section for a solution that way.

    The site uses the Smarty template system if that helps!

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BLazeD View Post
    However, it appears my site won't parse the PHP in the title tags for some reason,
    The PHP should parse anywhere in the document, post your code.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    Non-Member
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <title>- Example.com <?php echo ($_SERVER['PHP_SELF'] == '/index.php') ? '- The #1 Source for Example ' ''?>- {$pageDetail.page_name}</title>

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BLazeD View Post
    PHP Code:
    <title>- Example.com <?php echo ($_SERVER['PHP_SELF'] == '/index.php') ? '- The #1 Source for Example ' ''?>- {$pageDetail.page_name}</title>

    Try it the old fashioned way

    PHP Code:
    <title>- Example.com 
    <?php 
    if($_SERVER['PHP_SELF'] == '/index.php') {
       echo 
    ' - The #1 Source for Example ';

    ?>
    </title>
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    Non-Member
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, the page wouldn't even load that way

  10. #10
    SitePoint Enthusiast AnalogPanda's Avatar
    Join Date
    Jun 2006
    Location
    sacramento, ca, usa
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you have php?

  11. #11
    Non-Member
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah the site runs on it, I think that the templates system is stopping it from being parsed though

  12. #12
    SitePoint Enthusiast AnalogPanda's Avatar
    Join Date
    Jun 2006
    Location
    sacramento, ca, usa
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh right... my bad.

    I'd head over to the smarty forum and ask there - that's probably your best bet since smarty doesn't really use "normal" php tags and syntax.


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
  •