0

代码:

     <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 文件来完成此操作。如果这种方式不可行,还有哪些其他方式?假设我还想更改每个动态页面的元内容,我该怎么办?我也希望这对 SEO 友好。它是如何完成的?

4

4 回答 4

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>

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

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

于 2013-03-11T08:49:52.900 回答
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 上部),也可以在其中发送标头。以及下部的输出。使(在我看来)结构更加清晰。

于 2013-03-11T08:50:58.360 回答
0

如下所示。

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

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

在 HTML 代码之前定义变量。

于 2013-03-11T08:51:59.360 回答
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 或任何其他模板引擎。

于 2013-03-11T09:00:58.873 回答