【问题标题】:404 header in wordpress with php function带有php函数的wordpress中的404标头
【发布时间】:2020-12-28 16:50:33
【问题描述】:

我已经为我的 wordpress 网站建立了一个 404 页面。在我的404.php中,我用<?php get_header(); ?> 调用了标头函数。我想用 404 徽标替换我的正常徽标。在我的header.php 中,我添加了以下函数:

<?php

    if ( is_404()){
        echo "test";
    }
    else {
        echo "test1";
    }
        

?>

仅用于测试我是否可以对 404 页面执行其他操作并且它正在工作。

现在我想为 404 页面替换我的徽标。在这两种情况下,徽标都是svg。 这是header.php的相关部分:

    <header class="m-header">
        <div class="m-header-content">
            <div class="m-header-logo">
                 <a href="/">
                    <svg>
                        <title>test<title>
                        <path class="st0" d="svgpath....."/>
                    </svg>
                </a>
            </div>
        <div>
     
        <?php wp_nav_menu( array(
            'theme_location' => 'main_menu',
            'container_class' => 'm-header-menu') );
        ?>
    </header>

谁能给我一个例子,我如何在顶部更改&lt;svg&gt;?或者有人可以给我一份关于它的文档。

【问题讨论】:

    标签: php wordpress function svg header


    【解决方案1】:

    您可以在 header.php 的标记中使用条件,例如

                  <?php if (is_404()) { ?>
                       // Your 404 svg markup here.
                      <svg>
                       <title>404 Logo<title>
                         <path class="st0" d="svgpath....."/>
                      </svg>
                    <?php else { ?>
                     // Your regular logo svg here.
                     <svg>
                       <title>Site Logo<title>
                         <path class="st0" d="svgpath....."/>
                    </svg>
                  <?php } ?>
    

    在您提供的标题标记中,您可以这样应用 -

    <header class="m-header">
        <div class="m-header-content">
            <div class="m-header-logo">
                 <a href="/">
                    <?php if ( is_404() ) { ?>
                       // Your 404 svg markup here.
                      <svg>
                       <title>404 Logo<title>
                         <path class="st0" d="svgpath....."/>
                      </svg>
                    <?php else { ?>
                     // Your regular logo svg here.
                     <svg>
                       <title>Site Logo<title>
                         <path class="st0" d="svgpath....."/>
                    </svg>
                  <?php } ?>
                </a>
            </div>
        <div>
     
        <?php wp_nav_menu( array(
            'theme_location' => 'main_menu',
            'container_class' => 'm-header-menu') );
        ?>
    </header>
    

    附:对于错误的格式,通过电话回答。 希望这会有所帮助?

    【讨论】:

      【解决方案2】:

      假设您在顶部的 404 函数做正确的事情,它应该像这样工作。

      -&gt;SVG Path HTML 作为字符串,确保引号被转义[ " ] -&gt; [ \" ]

      <header class="m-header">
          <div class="m-header-content">
              <div class="m-header-logo">
                  <svg>
                      <?php
                      if ( is_404()){
                          echo "<title>404 svg</title>";
                          echo "<path class=\"st0\" d=\"svgpath.....\"/>"; // 404 SVG Path
                      } else {
                          echo "regular svg";
                          echo "<path class=\"st0\" d=\"svgpath.....\"/>"; // SVG Path
                      }
                      ?>
                      </svg>
                  </a>
              </div>
          <div>
          
          <?php wp_nav_menu( array(
              'theme_location' => 'main_menu',
              'container_class' => 'm-header-menu') );
          ?>
      </header>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-20
        • 2023-04-11
        • 1970-01-01
        • 2011-08-12
        • 2018-09-10
        相关资源
        最近更新 更多