首页 文章

如何从子主题中删除Wordpress主题选项

提问于
浏览
11

我通过为二十二主题编写一个儿童主题来开始新的项目 . 我很少设计新主题来使用二十二主题中内置的任何选项(例如背景颜色等) . 那些剩余的选择并没有真正伤害任何东西,但我想摆脱他们,因为他们什么都不做 .

问题是主题选项是在父主题的functions.php中声明的,它与子主题的functions.php文件一起加载(而不是代替)(因此我可以删除它们,但它们会在下次升级时返回) .

有没有办法从我的孩子主题中删除或禁用这些主题选项?或许类似于"remove_options()"功能的东西?或者可能达到这种效果的东西?换句话说, the question is whether theme_options can be removed WITHOUT deleting/overriding the original function that added them.

我肯定有足够的推特,我可以用CSS或javascript隐藏选项......但是来吧 .

4 回答

  • 13

    经过第二轮挖掘......

    这很容易!

    您可以通过启动here来回溯我的步骤,但代码非常明显:

    add_action( 'init', 'remove_crap' );
        function remove_crap() {
    
        remove_custom_image_header();
        remove_custom_background();
        remove_theme_support('post-formats');
    }
    

    您可以在手抄本中查看这些内容 . Remove_theme_support使用几个字符串中的一个来识别各种选项(除了后期格式) . 我遇到的唯一问题是它们需要从一个钩子中调用(你可以't just dumpt them into functions.php). I'使用 init 但是更多的是's probably another one that') .

    我唯一还有't figured out is how to remove the 1561776 page link that appears under Appearances. I know it'添加了 add_theme_page() ,但似乎没有一个方便的 remove_theme_page() .

    更新:我找到了!这是非常糟糕的文档,但最终它很容易做到:

    add_action('admin_init', 'remove_twentyeleven_theme_options', 11);
         function remove_twentyeleven_theme_options() {
     remove_submenu_page('themes.php', 'theme_options');
    }
    

    在我的例子中,'themes.php'定位于Appearances菜单,'theme_options'是二十二主题中使用的menu_slug . 显然,这些参数会根据您正在编辑的菜单或子菜单而有所不同 . This page会指出你正确的方向 .

    ps:这里's how to get rid of templates from the parent theme that you don' t想要使用:THIS isn 't essential to my exact question, but it' s密切相关,可能对任何's trying to do what I'做的人都有用 .

  • 1

    从子主题中删除父主题中添加的主题支持的正确方法是在调用的after_setup_theme操作中调用remove_theme_support,其优先级低于父项的优先级 .

    来自子主题的functions.php文件在父主题之前立即调用,因此如果您使用after_setup_theme的默认优先级,则子项的after_setup_theme最终会在父项之前调用,因此最终会删除不存在的您孩子的主题支持,只是从运行after_setup_theme的父项中重新添加 .

    因此,通过添加优先级较低的子操作,可以确保在父级调用相同操作后调用它 .

    所以:

    // added to child's functions.php    
    
    add_action( 'after_setup_theme', 'child_after_setup_theme', 11 ); 
    // Parent theme uses the default priority of 10, so
    // use a priority of 11 to load after the parent theme.
    
    function child_after_setup_theme()
    {
        remove_theme_support('custom-background');
        remove_theme_support('custom-header');
        remove_theme_support('post-formats');
        // ... etc.
    }
    

    在二十二主题的情况下,您也可以在您孩子的functions.php中覆盖整个twentyeleven_setup函数,但这是实现此目的的一种相当不明智的方法 .

  • 1

    不幸的是,主题继承在Wordpress案例中的工作方式是子主题函数只是“添加”到父主题函数 .

    与style.css不同,子主题的functions.php不会覆盖父对象的对应部分 . 相反,它除了父代的functions.php之外还被加载 . (具体来说,它是在父文件之前加载的 . )(1)

    因此,直接回答上面的问题,它看起来像(2)像WordPress处理主题和子主题的方式可能无法做到这一点 .

    就个人而言,我不担心在functions.php文件中有这些额外的函数或变量 .

  • 9

    这是一个旧线程,所以我只想添加一个人进入这个地方并想要一个答案 . 我通过获取该确切文件,制作副本并将其添加到子主题来解决子主题 . 我有插件“高级代码编辑器”所以我不需要进入FTP . 复制要编辑的特定文件,在子主题中创建具有相同名称和内容的新工作表,然后执行所需的编辑 . 它将首先获取子主题文件,并且您的站点将被更新 .

相关问题