Make if logic work to display php variable in custom HTML title

84 Views Asked by At

I am trying to incorporate logic into my website so that the $customTitle is working with the $nav1 variable. Below is how the page in question is structured.

<?
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
    include_once(G5_PATH."head.php");
?>

Currently, the HTML title is just showing as | Example.com so $nav1 is not being included.

I have a switch statement in head.php but because that is loaded after (needs to be) it doesn't actually get the data. Below is an example of that switch statement.

switch($gm_code){
    case "pd01" : $nav1 = "Category 1"; break;
    case "pd02" : $nav1 = "Category 2"; break;
    case "pd03" : $nav1 = "Category 3"; break;
    case "pd04" : $nav1 = "Category 4"; break;
    case "pd05" : $nav1 = "Category 5"; break;
}

So with this said, the final title would be Category 1 | Example.com.

I have tried to implement logic into this by using an if statement that looks at $gm_code and gives $nav1 different values based on that code. Below is an example of that.

<?
    if ($gm_code == "pd01") {
        $nav1 = "Category 1";
    }
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
    include_once(G5_PATH."head.php");
?>

How can I make this logic make sense and include the $nav1 variable so that it reflects the correct category name in the HTML <title>?

Edit:

I cannot just switch the order because it then defaults to the title in head.php which uses <title><?php echo $g5_head_title; ?></title>. I have incorporated the following if logic to make $customTitle work, do I have to switch this up to allow the order to change?

if (isset($customTitle)) {
  $g5['title'] = $customTitle;
  $g5_head_title = $customTitle;
} else if (!isset($g5['title'])) {
  $g5['title'] = $config['cf_title'];
  $g5_head_title = $g5['title'];
}
else {
  $g5_head_title = $g5['title'];
  $g5_head_title .= " | ".$config['cf_title'];
}
2

There are 2 best solutions below

3
On

Move head.php up so you set $nav1 before you try to use it in the value of $customTitle.

<?
    include_once(G5_PATH."head.php");
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
?>
1
On

I was able to get it to work using a specific order combined with if logic for all of the dynamic categories. As long as _common.php is loaded before the if statements and the $customTitle is loaded after the if statements and before the head.php, it works.

<?
    include_once("_common.php");

    if($gm_code == "pd01"){$nav1 = "Category 1";}
    if($gm_code == "pd02"){$nav1 = "Category 2";}
    if($gm_code == "pd03"){$nav1 = "Category 3";}
    if($gm_code == "pd04"){$nav1 = "Category 4";}
    ... etc

    $customTitle = "$nav1 | EXAMPLE.COM";
    include_once(G5_PATH."/_head.php");
?>

So for example, if the dynamic page I'm trying to load uses the $gm_code == "pd01" to get its content then the <title> is shown as Category 1 | EXAMPLE.COM.