更新动态页面的标题

时间:2013-03-11 08:46:30

标签: php html seo

代码:

     <html>
        <head>
        <title><?php echo $title; ?></title>
        </head>
        <body>
        <?php
            }if($_GET['page'] == 'Post'){
$title = "Post";
    ?>
    <div id = "contents">
    Post
    </div>

    <?php
            }else{
    ?>
        <div id = "contents">
$title = "Bla Bla Bla";
        Bla Bla Bla
        </div>
    <?php
            }
        }
    ?>
        </body>
        </html>

我知道我无法在头部之后定义变量,但我不知道该怎么做。我希望使用单个PHP文件完成此操作。如果这种方式无法实现,还有其他方法吗?假设我也想更改每个动态页面的Meta内容,我该怎么办?我也希望这是SEO友好。 怎么做?

4 个答案:

答案 0 :(得分:1)

您可以简单地将相同的if语句添加几行......

所以你的代码看起来像是:

<html>
    <head>
        <?php if(..your condition) { ?>
            <title>Show correct title>
        <?php } else { ?>
            <title>Other title, maybe with some meta tags..</title>
        <?php } ?>
    </head>

    <body>
        <?php if(..and the same condition again) { ?>
            <h1> And thus, it is the same condition </h1>
        <?php } ?>
    </body>
</html>

我希望你明白我在说什么; - )

编辑:请注意,我对此软件的设计并不“同意”。或许,缺乏设计。这只是简单问题的简单答案; - )

答案 1 :(得分:1)

如何重新排序并使用另外一个这样的变量:

<?php
  if($_GET['page'] == 'Post'){
    $title = 'Post'
    $content= '<div id = "contents">Post</div>';
  } else {
    $title = 'Bla Bla Bla';
    $content = '<div id = "contents">Bla Bla Bla</div>';
  }
?>
<html>
<head>
  <title><?php echo $title; ?></title>
</head>
<body>
  <?php echo $content; ?>
</body>
</html>

这样你就可以将你的页面分成计算(上面的PHP部分),也可以发送标题。而输出在下半部分。 (在我看来)使结构更加清晰。

答案 2 :(得分:0)

如下所示。

<?php
 $title = isset($_GET['page']) ? $_GET['page'] : 'normal heading';
?>

<html>
  <head><title><?=$title?></title></head>
</html>

在HTML代码之前定义变量。

答案 3 :(得分:0)

<?php ob_start(); /* open output buffer */ ?>
<html>
  <head><title>{title}</title></head>
[php/html code]
<?php
   $response = ob_get_clean(); /* get output buffer and clean */
   $metas = array('{title}' => $title, '{description}' => $description); 
   $response = strstr($response, '{title}', $title); 
   echo $response;

或使用smarty或任何其他模板引擎。