【问题标题】:Add an image to a header only on pages仅在页面上将图像添加到页眉
【发布时间】:2016-07-30 04:54:33
【问题描述】:

我正在使用 wordpress,我想仅在页面(不是帖子)上将图像添加到页眉。

你有这个功能吗?

谢谢

【问题讨论】:

    标签: wordpress image header


    【解决方案1】:

    主题是您自己创建的吗?如果是,您可以在要显示图像的地方使用此功能:

    <?php if(is_page()){
        // The image
    } ?>
    

    如果没有,您可以创建一个子主题。如果您在主题文件夹中找到page.php,请将此文件复制到您的子主题文件夹中。如果不存在,请搜索 singular.php,如果不存在则搜索 index.php。将其中一个复制到您的子主题文件夹中,并使用上面的代码if(is_page()){ 将图像放在那里。

    【讨论】:

      【解决方案2】:

      在你的主题->header.php中,寻找这段代码。它有 4 行(大约)。

      <?php $header_image = get_header_image();
      
       ( There are 2 lines in this area, just don't touch them )
      
      <?php endif; ?>
      

      找到后,像这样将我们的代码添加到顶行和底行

      <?php if( is_page() ) : ?>
      <?php $header_image = get_header_image();
      
       ( There are 2 lines in this area, just don't touch them )
      
      <?php endif; ?>
      <?php endif; ?>
      

      请记住,您正在实时编辑一个 php 文件,因此您必须非常小心,而且不用说您应该已经有备份,并且 FTP 登录以防万一出现问题。

      // When any single Page is being displayed.
      
      <?php if( is_page() ) : ?>
              <?php $header_image = get_header_image();
              if ( ! empty( $header_image ) ) : ?>
                  <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
              <?php endif; ?>
              <?php endif; ?><br>
      

      【讨论】:

      • 感谢您的详细解答。
      • 但我不确定我是否很好地解释了自己。我想在我的 function.php 文件中添加一个代码,以便将图像添加到菜单下方的标题中。就像页面是页面一样,添加图像“image.jpg”。你有类似的东西吗?非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多