简体   繁体   English

更新动态页面的标题

[英]Update Title for Dynamic Pages

Code: 码:

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

I know I can't define the variable after the head but I don't know what to do. 我知道我不能在头后定义变量,但我不知道该怎么做。 I want this to be done using a single PHP file. 我希望使用单个PHP文件完成此操作。 If this way is not possible, what are the other ways? 如果这种方式无法实现,还有其他方法吗? Lets say I also want to change the Meta contents for each dynamic page, what do I do? 假设我也想更改每个动态页面的Meta内容,我该怎么办? I also want this to be SEO friendly. 我也希望这是SEO友好。 How is it done? 怎么做?

You can simply add the same if-statement a few lines higher too.. 您可以简单地将相同的if语句添加几行。

So your code would look like, ie: 所以你的代码看起来像,即:

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

I hope you understand what I am saying ;-) 我希望你明白我在说什么;-)

edit : Note that I do not "agree" on the design of this piece of software. 编辑 :请注意,我不同意这个软件的设计。 Or maybe, lack of design. 或许,缺乏设计。 It's just the simple answer to the simple question ;-) 这只是简单问题的简单答案;-)

How about some reordering and using one more variable like this: 如何重新排序并使用另一个变量如下:

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

That way you split your page into computation (the upper PHP part), where also headers can be send. 这样你就可以将页面分成计算(上面的PHP部分),也可以发送标题。 And the output in the lower part. 而输出在下半部分。 Makes (in my opinion) for a much clearer structure. (在我看来)使结构更加清晰。

Do it as below. 如下所示。

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

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

Define variable before HTML code. 在HTML代码之前定义变量。

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

Or use smarty or any other template engines. 或者使用smarty或任何其他模板引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM