简体   繁体   English

在 WordPress 管理仪表板中加载自定义页面模板

[英]Load custom page templates in WordPress admin dashboard

I have several custom page templates in my theme.我的主题中有几个自定义页面模板。 But I want to hide a few with a plugin, and only show the custom home page template on the page that has been set as the front page "is_front_page" or "is_home".但是我想用插件隐藏一些,并且只在已设置为首页“is_front_page”或“is_home”的页面上显示自定义主页模板。 This is what I am using in my plugin to stop some of the page templates from showing up.这就是我在我的插件中用来阻止某些页面模板显示的内容。

This is for a large multisite where there are two tiers of sites, one gets the full set of features, and the other a stunted set.这适用于有两层站点的大型多站点,一层具有完整的功能集,另一层是发育不良的集。 I have everything working as I need except for this.除了这个,我的一切都可以按我的需要工作。

add_filter( 'theme_page_templates', 'je_remove_page_template' );
  function je_remove_page_template( $pages_templates ) {
  unset( $pages_templates['page-topimage.php'] );
  unset( $pages_templates['page-home.php'] );
  return $pages_templates;
}

The code above works totally fine, but I need a conditional in there to show the page-home.php when the page has been set to the home page.上面的代码完全正常,但是当页面被设置为主页时,我需要一个条件来显示 page-home.php。 I have tried this code, but it doesn't work.我试过这段代码,但它不起作用。

if ( is_front_page() ) :

    add_filter( 'theme_page_templates', 'je_remove_page_template' );
    function je_remove_page_template( $pages_templates ) {
    unset( $pages_templates['page-topimage.php'] );
    return $pages_templates;
    }

    else :

    function je_remove_page_template( $pages_templates ) {
    unset( $pages_templates['page-topimage.php'] );
    unset( $pages_templates['page-home.php'] );
    return $pages_templates;
    }

endif;

Any ideas on how I can get this to work?关于如何让它发挥作用的任何想法?

A few things:一些东西:

One, it appears you've only added theadd_filter call to the is_front_page() 's true condition, that's probably part of it.一,您似乎只将add_filter调用添加到is_front_page()true条件,这可能是其中的一部分。

Second, you should not be defining and/or redefining functions inside if statements!其次,你应该定义和/或重新定义函数内部if语句!

Third, for WordPress specifically, with it's Action Hook and Filter system, it's generally considered a best practice to add the filters and actions at all times, and run if/else or abort type statements inside the function to allow for easier extensibility.第三,特别是对于 WordPress,由于它的Action Hook 和 Filter系统,通常认为最佳做法是始终添加过滤器和操作,并在函数内运行if/else或 abort 类型语句以允许更容易的可扩展性。

Edit:编辑:

Based on some clarification, I understand a bit better.根据一些澄清,我理解得更好一些。 You need to get the ID of the page that's set to the front page, which is stored as an option named page_on_front in the options table.您需要获取设置为首页的页面的 ID,该 ID 存储为选项表中名为page_on_front的选项。 The is_front_page() function will only work in the scope of the current $wp_query loop. is_front_page()函数只会在当前$wp_query循环的范围内工作。

I think this would solve it for you.我想这会为你解决它。 Run the je_remove_page_template function on the theme_page_templates filter, and check the current page's ID against the one stored in the page_on_front option:theme_page_templates过滤器上运行je_remove_page_template函数,并根据page_on_front选项中存储的 ID 检查当前页面的 ID:

add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ){
    unset( $pages_templates['page-topimage.php'] );

    $home_page_id = absint( get_option( 'page_on_front' ) );
    $this_page_id = get_the_ID();

    // If this isn't the home page, remove `page-home`
    if( $this_page_id != $home_page_id ){
        unset( $pages_templates['page-home.php'] );
    }

    return $pages_templates;
}

Also of note would be that is_front_page() is different than is_home() - make sure you're using the correct one!另外值得注意的是is_front_page()is_home()不同 - 请确保您使用的是正确的! (I believe you are, as generally IFP is what people want) (我相信你是,因为通常 IFP 是人们想要的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM