代码:
<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>
我知道我不能在 head 之后定义变量,但我不知道该怎么做。我希望使用单个 PHP 文件完成此操作。如果这种方式不可行,还有哪些其他方式?假设我还想更改每个动态页面的 Meta 内容,我该怎么做?我也希望这是 SEO 友好的。 它是怎么做到的?
最佳答案
像这样重新排序并使用一个变量如何:
<?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 部分),其中还可以发送 header 。以及下部的输出。使(在我看来)结构更加清晰。
关于php - 更新动态页面的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334324/