代码之家  ›  专栏  ›  技术社区  ›  Cole

更新动态页面的标题

  •  0
  • Cole  · 技术社区  · 13 年前

    代码:

         <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 回复  |  直到 13 年前
        1
  •  1
  •   Sirko    13 年前

    不如重新排序,再使用一个这样的变量:

    <?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
  •  1
  •   Niek van der Steen    13 年前

    您也可以简单地将相同的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>
    

    我希望你能理解我的意思;-)

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

        3
  •  0
  •   Dipesh Parmar    13 年前

    按以下步骤操作。

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

    在HTML代码之前定义变量。

        4
  •  0
  •   selam    13 年前
    <?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或任何其他模板引擎。

    推荐文章