Rahul Vyas
Rahul Vyas

Reputation: 28720

how to add Title, keyword and description different on every page of simple php website dynamically

How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?

E.g

<title>this is title</title>
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="this is description" />

Upvotes: 1

Views: 7247

Answers (1)

Jakub Arnold
Jakub Arnold

Reputation: 87220

You can create method to return title for current page, depending on where the user is, and then use it like this.

<title><?php echo get_title(); ?></title>

same with the keywords

<meta name="keywords" content="<?php echo get_keywords(); ?>" />
<meta name="description" content="<?php echo get_description(); ?>" />

The implementation will depend on how you navigate on the website. For example, if you have only index.php, and choose content by $_GET["page"], you can have something like this

function get_title() {
   switch($_GET["page"]) {
      case "home":
         return "Welcome to my home page";
      case "guestbook":
         return "Welcome to guestbook";
   }
}

or you can make it all in one like

function get_headers() {
    // here set $title, $description and $keywords according to current page 
    // ....

    // then just generate html
    $html = "<title>$title</title>";
    $html .= "<meta name='description' content='$description' />";
    $html .= "<meta name='keywords' content='$keywords' />";

    return $html;
}

and then again do something like this

<head>
    ...
    <?php echo get_headers(); ?>
    ...

Upvotes: 2

Related Questions