Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP if/else语句编写div_Php_If Statement - Fatal编程技术网

使用PHP if/else语句编写div

使用PHP if/else语句编写div,php,if-statement,Php,If Statement,这是来自我正在工作的wordpress网站,但不是一个特定于WP的问题 我有一个if/else PHP语句,检查用户是否正在查看我的站点主页。如果他们正在查看主页,我希望代码什么也不做。如果没有,我希望它在配置中显示页面标题 我目前拥有的代码是: <div class="page-header"> <h1> <?php if ( is_front_page()){ echo ' '; } elseif (is_ho

这是来自我正在工作的wordpress网站,但不是一个特定于WP的问题

我有一个if/else PHP语句,检查用户是否正在查看我的站点主页。如果他们正在查看主页,我希望代码什么也不做。如果没有,我希望它在配置中显示页面标题

我目前拥有的代码是:

<div class="page-header">
  <h1>
    <?php
    if ( is_front_page()){
        echo ' ';
    }
    elseif (is_home()) {
        if (get_option('page_for_posts', true)) {
          echo get_the_title(get_option('page_for_posts', true));
        } else {
          _e('Latest Posts', 'roots');
        }
      } elseif (is_archive()) {
        $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
        if ($term) {
          echo $term->name;
        } elseif (is_post_type_archive()) {
          echo get_queried_object()->labels->name;
        } elseif (is_day()) {
          printf(__('Daily Archives: %s', 'roots'), get_the_date());
        } elseif (is_month()) {
          printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
        } elseif (is_year()) {
          printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
        } elseif (is_author()) {
          global $post;
          $author_id = $post->post_author;
          printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
        } else {
          single_cat_title();
        }
      } elseif (is_search()) {
        printf(__('Search Results for %s', 'roots'), get_search_query());
      } elseif (is_404()) {
        _e('File Not Found', 'roots');
      } else {
        the_title();
      }
    ?>
  </h1>
</div>

我知道回声可能还差得远,但我绝对是php初学者!目前,这会创建一个空的
标记,但我宁愿清理它,在主页上什么也不创建


如何最好地修改上面的代码来实现这一点?

将回音放入变量中,而不是打印它们,然后在if语句之后执行

echo($var='')“”$变种'


最后两组单引号之间的Html标记需要移动代码。如果您不想在头版上打印前导的
,请在前面移动
If
复选框。添加一个
else
,然后在其中打印
、大的if/else代码块和结束的
,等等

也请阅读。
尽管如此,最好的解释可能是展示:

<?php
    if ( is_front_page()){
        echo ' ';
    }
    else {
?>
<div class="page-header">
  <h1>
    <?php

    if (is_home()) {
        if (get_option('page_for_posts', true)) {
          echo get_the_title(get_option('page_for_posts', true));

       /*

           ...

       */

      } else {
        the_title();
      }
    ?>
  </h1>
</div>
<?php
     }
?>


这里唯一有趣的是,您将html+代码块括在
else
的开头
{
和结尾
}
花括号之间。

为什么不删除
else
并使用
if(!is_front_page()){
?OP只是不想在页面上显示任何内容,所以回显空格是不必要的。OP看起来很混乱,所以我放弃了否定,还保留了多余的空
echo
。它只是展示了结构。哈哈,谢谢这些家伙。我确实想过做类似的事情,但没想到你可以把html扔进PH中P if/else语句。
if(!is_front_page()){
看起来很简单,所以我会用它。干杯,伙计们!