我正在使用TwentyTen主题创建子主题,但似乎无法摆脱TwentyTen父主题中的“一个列,没有侧边栏”页面模板。

我以为只复制它并删除内容就可以解决问题,但似乎没有。有谁知道如何做到这一点?我敢肯定这很简单。

感谢

osu

#1 楼

覆盖该模板比摆脱它要容易得多。逻辑就是这样。

我没有断言它是有效的想法(在这里晚些时候),但这会使它从编辑屏幕中消失:


评论


感谢您这样做,这似乎是一个不错的解决方案-当您说覆盖模板时,是否意味着无论如何都只在其中包含一列模板?还是用我想实际使用的模板覆盖它(即称它为一栏,但实际上却有2或3栏?)?

–大须
2011年4月1日上午11:49

@Osu我的意思是使用您自己的相似模板进行覆盖。就逻辑而言,很容易更改子主题并将其添加到父主题的模板,但是很难禁用它们。基本上,子主题应该比父主题做更多,而不是更少。代码逻辑遵循此原理。

–稀有
2011年4月1日在12:24

喔好吧。感谢您清理。然后,我将为他们创建一个col模板。干杯

–大须
2011年4月5日上午9:42

对于任何想使用jQuery从下拉列表中删除主题的人,那么这是另一种方法(虽然不确定这是否是最好的方法):pastie.org/3710683

–大须
2012年4月1日20:06



@brasofilo与主题相关的内部组件在3.4中进行了重大的重构和API更改,因此许多较旧的内容将不起作用

–稀有
2012年7月9日在21:46

#2 楼

WordPress 3.9引入了theme_page_templates过滤器。

下面的“二十四岁”子主题functions.php中的示例显示了如何删除“参与者页面”模板:

function tfc_remove_page_templates( $templates ) {
    unset( $templates['page-templates/contributors.php'] );
    return $templates;
}
add_filter( 'theme_page_templates', 'tfc_remove_page_templates' );


评论


这应该是WP 3.9+的更新的接受答案

–helgatheviking
2015年12月18日在20:25

#3 楼

扩展@Rarst的答案,这是一种更通用的方法,与特定主题无关,但是可以在您自己的子主题的functions.php中使用,以核对您想要摆脱的任何父主题页面模板。

function remove_template( $files_to_delete = array() ){
    global $wp_themes;

    // As convenience, allow a single value to be used as a scalar without wrapping it in a useless array()
    if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );

    // remove TLA if it was provided
    $files_to_delete = preg_replace( "/\.[^.]+$/", '', $files_to_delete );

    // Populate the global $wp_themes array
    get_themes();

    $current_theme_name = get_current_theme();

    // Note that we're taking a reference to $wp_themes so we can modify it in-place
    $template_files = &$wp_themes[$current_theme_name]['Template Files'];

    foreach ( $template_files as $file_path ){
        foreach( $files_to_delete as $file_name ){
            if ( preg_match( '/\/'.$file_name.'\.[^.]+$/', $file_path ) ){
                $key = array_search( $file_path, $template_files );
                if ( $key ) unset ( $template_files[$key] );
            }
        }
    }
}


因此您可以在子主题的functions.php文件中使用它,如下所示:

add_action( 'admin_head-post.php', 'remove_parent_templates' );

function remove_parent_templates() {
    remove_template( array( "showcase.php", "sidebar-page" ) );
}

只是说明您不需要,不必传递“ .php”部分。

或者:remove_template( "sidebar-page" );-如果要修改,则不需要传递数组只有一个文件。

#4 楼

WP core(3.9)中有一个新过滤器,用于删除页面模板。可以从子主题中使用它。

这是在TwentyTen中实现此目的的方法(在WP 3.9上测试):

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


https:/ /core.trac.wordpress.org/changeset/27297

http://boiteaweb.fr/theme_page_templates-hook-semaine-16-8033.html

评论


如果链接发生更改或断开,则链接异地链接将变得无用。这也没有尝试真正回答问题

– Tom J Nowell♦
14年4月17日在22:25

足够公平,添加了示例。

– Marcio Duarte
2014年4月18日12:30在

#5 楼

由于以前的答案在WordPress的当前版本中不再适用于此,并且还有一个相关问题,我刚刚使用PHP输出缓冲区回答了此问题(2013年4月),所以我想我会发布指向该答案的链接。

还刚刚发布了“省略父主题页面模板”插件,该插件在添加或编辑WordPress“页面”时从“页面属性”元框中的模板下拉列表中过滤掉所有父主题页面模板。

#6 楼

2012年7月10日-WordPress 3.4.1

先前的答案不起作用,正如Rarst在评论中所说:


与主题相关的内部结构经历了重大重构和API在3.4中进行了更改,因此许多较旧的内容将无法正常运行

如果我遵循正确的路径,这就是发生“动作”的地方(/wp-includes/class-wp-theme.php),看起来这里没有什么可以勾住的...

add_action('admin_head', 'wpse_13671_script_enqueuer');

function wpse_13671_script_enqueuer() {
    global $current_screen;

    /**
     * /wp-admin/edit.php?post_type=page
     */
    if('edit-page' == $current_screen->id) 
    {
        ?>
        <script type="text/javascript">         
        jQuery(document).ready( function($) {
            $("a.editinline").live("click", function () {
                var ilc_qe_id = inlineEditPost.getId(this);
                setTimeout(function() {
                        $('#edit-'+ilc_qe_id+' select[name="page_template"] option[value="showcase.php"]').remove();  
                    }, 100);
            });

            $('#doaction, #doaction2').live("click", function () {
                setTimeout(function() {
                        $('#bulk-edit select[name="page_template"] option[value="showcase.php"]').remove();  
                    }, 100);
            });       
        });    
        </script>
    <?php
    }

    /**
     * /wp-admin/post.php?post=21&action=edit
     */
    if( 'page' == $current_screen->id ) 
    {
        ?>
        <script type="text/javascript">
        jQuery(document).ready( function($) {
            $('#page_template option[value="showcase.php"]').remove();
        });
        </script>
    <?php
    }
}


评论


Trac票证:core.trac.wordpress.org/ticket/21891

–mrwweb
2012年9月14日17:44