基于URL生成动态元标记

时间:2012-05-21 17:16:41

标签: php dynamic meta-tags

我正在尝试为每个页面动态分配动态独占的元标记:

即: 的 url.com/index.php?action=signup

标题 - 注册标题

关键字 - 注册元关键字

描述 - 注册说明

url.com/index.php?action=about

标题 - 关于标题

关键字 - 关于元关键字

描述 - 关于描述

你明白了。

我正在使用数组...但是还没想到如何将每个数组分配到每个页面。

CONF.PHP

<?php
$metas = array( 
'index.php' => array( 
'header' => 'Home Title', 
'keywords' => 'Home Meta Keywords', 
'description' => 'Home Meta Description' 
), 
'signup' => array( 
'header' => 'Signup Title', 
'keywords' => 'Signup Meta Keywords', 
'description' => 'Signup Meta Description' 
), 
'about' => array( 
'header' => 'About Title', 
'keywords' => 'About Meta Keywords', 
'description' => 'About Meta Description' 
) 
);
?>

的index.php

<TITLE><?php echo $metas['title']; ?></TITLE>
<meta name="description" content="<?php echo $metas['description']; ?>" >
<meta name="keywords" content="<?php echo $metas['keywords']; ?>" >

如何将这些值分配到每个页面???

1 个答案:

答案 0 :(得分:6)

你走在正确的轨道上: - )

以下列方式进行:

<TITLE><?php echo $metas['index.php']['title']; ?></TITLE>

脚本名称,您可以从$ _SERVER超级全局数组/变量中获取它。

同样从你的config.php文件中删除最后3行,你不需要它们。

祝你好运,用PHP确保你对使用数组有很好的了解。这是关键。

编辑:

$page = 'index.php';
if ( isset( $_GET['action'] ) && $_GET['action'] != "" )
{
    $page = $_GET['action'];
}

<TITLE><?php echo $metas[$page]['title']; ?></TITLE>

编辑:( 2012年5月27日)

我认为这几乎就是一切:

您在config.php中的代码

<?php

$meta['INDEX']['title'] = "Home page";
$meta['INDEX']['keywords'] = "kwd1, kwd2, kwd3";
$meta['INDEX']['description'] = "Home description";

$meta['SIGNUP']['title'] = "Sign up..!";
$meta['SIGNUP']['keywords'] = "kwd1, kwd2, kwd3";
$meta['SIGNUP']['description'] = "Sign up description";

$meta['ABOUT']['title'] = "About Company";
$meta['ABOUT']['keywords'] = "kwd1, kwd2, kwd3";
$meta['ABOUT']['description'] = "About company description";

您的任何应用程序页面/屏幕(.php文件)中的代码:

<?php

include "config.php";

// Choice 1: if you want to specify page hardcoded at each page then you can say $page = 'ABOUT' for about-us page and $page = 'INDEX' for home page etc.
$page = 'ABOUT';
if( strtoupper($_SERVER['REQUEST_URI'], $page) !== false )
{
    $title = $meta[$page]['title'];
    $keywords = $meta[$page]['keywords'];
    $description = $meta[$page]['description'];
}
// Now you have your meta - use it the way you want
echo $title;

// Choice 2: If you want to make it little more dynamic
// Here you don't need to define any hardcoded variable at page level as everything will be considered from the URL being requested
$page_index = array_keys($meta);

foreach($page_index as $page)
{

    if ( strpos( strtoupper($_SERVER['REQUEST_URI']), $page ) !== false)
    {
        $title = $meta[$page]['title'];
        $keywords = $meta[$page]['keywords'];
        $description = $meta[$page]['description'];
        break;
    }
}
// Now you have your meta - use it the way you want
echo $title;

?>