Custom Chaging Page Title with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StarWallace
    New Member
    • Jul 2010
    • 5

    Custom Chaging Page Title with php



    I assume that this should be quite easy but i just cant figure this out at all... I'm trying to make it so that the title of the page changes depending on the page that is included into my base index.php.

    i have things set up within the index.php so that i use this code
    Code:
    		<?php
    		
    			if (!isset($_GET['page'])) {
    			$page = "blog"; 
    			} else {
    			$page = $_GET['page'];
    			}
    			
    			include ("pages/".$page.".php");
    
    		?>
    to include the various pages from my pages directory.

    what i want to do is be able to have the viewer be able to browse to, say, the Games page and have that reflect in the title. like: MicroStudios - Games - Best Viewed In Firefox

    the problem i keep mindlessly running into is that the include page is of course always rendered AFTER the page title has been set... every site i've browsed through did not have a solution other than javascript and i'd like to avoid that 'cause that causes SEO problems...
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Originally posted by StarWallace
    http://microstudios.co.cc/

    I assume that this should be quite easy but i just cant figure this out at all... I'm trying to make it so that the title of the page changes depending on the page that is included into my base index.php.

    i have things set up within the index.php so that i use this code
    Code:
    		<?php
    		
    			if (!isset($_GET['page'])) {
    			$page = "blog"; 
    			} else {
    			$page = $_GET['page'];
    			}
    			
    			include ("pages/".$page.".php");
    
    		?>
    to include the various pages from my pages directory.

    what i want to do is be able to have the viewer be able to browse to, say, the Games page and have that reflect in the title. like: MicroStudios - Games - Best Viewed In Firefox

    the problem i keep mindlessly running into is that the include page is of course always rendered AFTER the page title has been set... every site i've browsed through did not have a solution other than javascript and i'd like to avoid that 'cause that causes SEO problems...
    $_GET['page'] is available immediately, simply use it for your title. I am not sure what is your issue here. Can you provide more code? Especially the part where your title is defined.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Is your title a variable? It should be accessible throughout unless you're using a function?

      Comment

      • Samishii23
        New Member
        • Sep 2009
        • 246

        #4
        Code:
        <?php
        switch ( @$_GET['page'] ) {
          case "":
            include "blog.php"; break;
          case "games":
            include "games.php"; break;
          }
        ?>

        Comment

        • StarWallace
          New Member
          • Jul 2010
          • 5

          #5
          I had been trying to use something along the lines of

          HEAD
          Code:
          <title>MicroStudios - <?php echo $title; ?> - Best viewed in Firefox</title>
          INCLUDED BODY PAGE
          Code:
          <?php $title="About"; header($title); ?>
          but that doesn't work because the included "About" page is loaded after the index.php has already declared the title.

          i don't know php well enough to have any idea of what i'm doing right or wrong... so that's all i really know to tell you. I'm TRYING to set a custom title from within the included page... if that's possible...

          Comment

          • Samishii23
            New Member
            • Sep 2009
            • 246

            #6
            Code:
            <?php
            switch ( @$_GET['page'] ) {
              case "":
                include "blog.php";
                $title = "blog"; break;
              case "games":
                include "games.php";
                $title = "Games"; break;
              }
            ?>
            
            <title>MicroStudios - <?php echo $title; ?> - Best viewed in Firefox</title>

            Comment

            • StarWallace
              New Member
              • Jul 2010
              • 5

              #7
              i'm not sure i understand how that would work... doesn't the <title> tag have to be set in the <head>?

              and besides that i don't think it would work for everything i'm trying to do because sometimes i want to be able to take the title from other things in the page. For example, a specific blog post, i don't know the name of the post until it has been loaded so i can't pre set the title for that...

              it would be nice if you would give some explanation on what your showing me to do with the code you posted... i don't know php very well yet, i still only know extremely basic stuff.

              edit: also, because i only know such simple stuff... i don't know the difference between a variable and a function...

              Comment

              • zorgi
                Recognized Expert Contributor
                • Mar 2008
                • 431

                #8
                can you give us intire code of your index.php?

                Comment

                • StarWallace
                  New Member
                  • Jul 2010
                  • 5

                  #9
                  Code:
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  
                  
                  
                  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
                  <head>
                  
                  
                  
                  <!-- <title>MicroStudios - <?php echo $title; ?> - Best viewed in Firefox</title>-->
                  <meta content="text/php; " http-equiv="Content-Type"/>
                  <meta content="all" name="audience"/>
                  <meta content="global" name="distribution"/>
                  <meta content="INDEX, FOLLOW" name="robots"/>
                  <meta content="7 days" name="revisit-after"/>
                  <meta content="MicroStudios is a freeware games website. All games are freeware and are made with a variety of programing languages." name="description"/>
                  <meta content="MicroStudios, Micro, Studios, StarWallace, Robbi, Grabnar, Grabnar the Wanderer, Games, XPlay, X-play, GrabnarGames" name="keywords"/>
                  <link rel="stylesheet" type="text/css" href="<?php echo $stylesheet;  ?>" />
                  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
                  <script type="text/javascript" src="js/gradualfader.js"></script>
                  <script type="text/javascript" src="slimbox/js/slimbox2.js"></script>
                  <script type="text/javascript" src="js/reflection.js"></script>
                  <script type="text/javascript" src="js/ddwindowlinks.js"></script>
                  <link rel="stylesheet" href="slimbox/css/slimbox2.css" type="text/css" media="screen" />
                  
                  </head>
                  
                  <body>
                  <!--[if lte IE 6]>
                  It appears that you are using Internet Explorer 6 or earlier. This website may not function fully on any version of IE before IE7. We appologize for the trouble, please update your browser or try out <a href="http://firefox.com">Firefox</a>!
                  <![endif]--> 
                  	<div id="Top"> <!-- The top section containing the header image, the menu, and a possible ad -->
                  		<img src="css/images/head.png" style="margin: 0px 0px 0px -20px;"> 
                  
                  
                  		<div id="Menu">
                  			<a href="index.php?">Blog</a> <b>|</b> <a href="index.php?page=games">Games</a> <b>|</b> <a href="index.php?page=examples">Examples</a> <b>|</b> <a href="index.php?page=about">About</a> <b>|</b> <a href="index.php?page=contact">Contact</a>
                  		</div>
                  
                  	</div>
                  
                  	<div id="Content"> <!-- The content section holding a possible side bar and the main content -->
                  
                  
                  
                  
                  		<?php
                  		
                  			if (!isset($_GET['page'])) {
                  			$page = "blog"; // Default page
                  			} else {
                  			$page = $_GET['page'];
                  			}
                  			
                  			include ("pages/".$page.".php");
                  
                  		?> 
                  		
                  </div>
                  	
                  
                  	<div id="Footer" > <!-- the footer for copyright text and other things -->
                  	<a title="RSS Feed" target="_blank" href="/pages/blog/rss.php?number=10"><img src="/pages/blog/skins/images/rss_icon.gif" style="margin: 0px 0px -4px 0px; border-width:0;" title="Subscribe to MicroStudios' blog's RSS feed"border=0  /></a>
                  	<a href="http://www.firefox.com/" target="_blank"><img src="http://i33.tinypic.com/w9gtgx.png" style="margin: 0px 0px -4px 0px; border-width:0;" alt="Firefox" title="I recommend Firefox for the best internet browsing experience"></a>
                  	<b>Copyright © 2006 - 2010 MicroStudios. Some Rights Reserved.</b>
                  	<a href="index.php?page=about#copyright"><img alt="Creative Commons License" style="margin: 0px 0px -4px 0px; border-width:0;" src="http://i50.tinypic.com/2dl3ibm.png" title="MicroStudios copyright information"/></a>
                  	<a href='http://host-tracker.com/' target="_blank" onMouseOver='this.href="http://host-tracker.com/uptime-statistics/2894124/lvuc/";'><img style="margin: 0px 0px -4px 0px; border-width:0;" title="MicroStudios' total uptime" width='80' height='15' border='0' alt='online check' src="http://ext.host-tracker.com/uptime-img/?s=15&amp;t=2894124&amp;m=00.59&amp;p=Total&amp;src=lvuc" /></a><noscript><a href='http://host-tracker.com/' target="_blank">broadband speed test</a></noscript>        
                  	</div>
                  <script type="text/javascript">
                  gradualFader.init() //activate gradual fader
                  </script>
                  
                  </body>
                  
                  </html>
                  i hope that helps...

                  Comment

                  • StarWallace
                    New Member
                    • Jul 2010
                    • 5

                    #10
                    anyone? doesn't anyone know how to fix this?

                    Comment

                    • zorgi
                      Recognized Expert Contributor
                      • Mar 2008
                      • 431

                      #11
                      Samishii23 alredy answered your question.

                      Comment

                      Working...