【问题标题】:Best way to omit elements from Wordpress header/footer on certain pages在某些页面上从 Wordpress 页眉/页脚中省略元素的最佳方法
【发布时间】:2019-01-03 13:39:54
【问题描述】:

我的 Wordpress 网站上的大多数页面都有相同的页眉和页脚。标题包括表单和菜单以及联系按钮。页脚还包含一个表单。

我有许多完全自定义布局的页面。例如。 page-personal-training-landing.php & page-beach-body-ready.php。对于这些页面,我省略了页眉和页脚中的元素。

目前我正在使用“如果不是”语句在所有页面上显示这些元素,除了那些自定义页面...

<?php if ((!is_page( 'contact' ))&&(!is_page('beach-body-landing'))&&(!is_page('4-week-body-transformation'))&&(!is_page('beach-body-ready'))&&(!is_page('personal-training-landing'))){?>
<div class="contact-button"></div>
<?php } ?>  

...但是正如您所见,这段代码变得很长,我会在创建新页面时不断向其中添加新页面。

我认为模板是解决方案.. if(!is_page_template( 'noheadernofooter-page.php' )... 但不幸的是,Wordpress 在默认为自定义页面布局之前默认为该模板布局...

Wordpress 页面模板选择的层次...

  1. 自定义模板文件 – 分配给页面的页面模板。看 get_page_templates()。
  2. page-{slug}.php - 如果页面 slug 是最近的新闻,WordPress 将 看看使用 page-recent-news.php。
  3. page-{id}.php – 如果页面 ID 为 6,WordPress 将使用 page-6.php。
  4. page.php
  5. singular.php
  6. index.php

我现在在想,也许我应该使用高级自定义字段来添加一些复选框。例如。隐藏页眉按钮(Y/N),然后查询当前页的值。

这听起来是不是最合乎逻辑的解决方案?

有谁知道在没有其他插件的情况下通常如何做到这一点?

谢谢

【问题讨论】:

  • 这并不能解决问题,但您知道您可以将一组 ID 传递给 is_page(),对吧? (例如if ( ! is_page( array(62, 64, 78, 123) ) ) { // do some stuff })。
  • 如果这些按钮只显示在固定数量的页面上,那么我会反过来:if ( is_page(array(23, 25, 60)) ) { // display buttons }

标签: php html wordpress


【解决方案1】:

这是另一种方式,使用前面提到的get_header()

获取 post slug 并将其传递给 get_header。这样,您的页面将始终查找自定义头文件,但如果找不到,WP 将默认使用标准 header.php:

if ( is_page() ) {
    global $post;
    get_header( $post->post_name );
}

这确实意味着您需要为每个页面自定义 header.php 文件,因此您最终可能会得到数百个头文件,但您可以通过使用符号链接来规避这一点。

【讨论】:

    【解决方案2】:

    在我看来,你这样做的方式已经是正确的方式。我可能会添加包含以使其更易于维护,但我认为 if 语句是要走的路。以我使用过的一些代码(针对 OP 修改)为例:

    if( (is_page( 'contact' ) ) {
    
    include( 'header-contact.php' ); // include this file on contact page
    
    } elseif( is_page('beach-body-landing') ) {
    
    include( 'header-body-landing.php' ); // include that file on body landing page
    
    } else {
    
    get_header(); // otherwise use our normal header
    
    }
    

    不幸的是,没有钩子可以连接到get_header()get_footer()get_sidebar(),因为这样我们就可以在一个地方完成所有操作。这正是 wordpress 开发的本质。

    编辑

    要包括您在下面提到的内容:您可以像这样调整:

    if( (is_page( 'contact' ) ) {
    
    get_header('contact'); // use `header-contact.php`
    
    } elseif( is_page('beach-body-landing') ) {
    
    get_header('body-landing'); // use `header-body-landing.php`
    
    } else {
    
    get_header(); // otherwise use our normal header
    
    }
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我认为我在这里找到了最优雅的解决方案...

      https://stackoverflow.com/a/36521754/3656408

      “你可以创建新的页眉和页脚文件,比如“header-mytemplate.php”和“footer-mytemplate.php”。创建你想要的任何结构。在你的新模板中调用这些页眉和页脚,比如这个 get_header('mytemplate'); 和 get_footer(mytemplate);。"

      如果有人认为有更好的解决方案,我仍然想知道

      【讨论】:

        猜你喜欢
        • 2021-06-22
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        相关资源
        最近更新 更多