3

我正在尝试为每个页面动态分配专有元标记:

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

标题 - 注册标题

关键字 - 注册元关键字

说明 - 注册说明

url.com/index.php?action=about

标题 - 关于标题

关键字 - 关于元关键字

说明 - 关于说明

你明白了。

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

配置文件

<?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' 
) 
);
?>

索引.PHP

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

如何将这些值分配给每一页???

4

1 回答 1

8

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

请按以下方式进行:

<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;

?>
于 2012-05-21T17:56:46.993 回答