尝试使用重写的函数时出现21个子主题错误

时间:2012-10-12 作者:Nohl

我试图对《二十一世纪主题》(wordpress 3.4.2)中的一个函数做一个小改动

我收到以下错误消息:

Fatal error: Cannot redeclare twentyeleven_widgets_init() (previously declared in /home/labfocus/public_html/wp-content/themes/twentyeleven-child/functions.php:10) in /home/labfocus/public_html/wp-content/themes/twentyeleven/functions.php on line 438
下面是我试图添加的函数,以代替原来的

<?php
if ( !function_exists(\'twentyeleven_widgets_init\')) {
/**
 * Register our sidebars and widgetized areas. Also register the default Ephemera widget.
 *
 * @since Twenty Eleven 1.0
 */
function twentyeleven_widgets_init() {

    register_widget( \'Twenty_Eleven_Ephemera_Widget\' );

    register_sidebar( array(
        \'name\' => __( \'Main Sidebar\', \'twentyeleven\' ),
        \'id\' => \'sidebar-1\',
        \'before_widget\' => \'<div class="su-spoiler-title">%2$s</div><aside id="%1$s" class="widget %2$s">\',
        \'after_widget\' => "</aside></div>",
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );

    register_sidebar( array(
        \'name\' => __( \'Showcase Sidebar\', \'twentyeleven\' ),
        \'id\' => \'sidebar-2\',
        \'description\' => __( \'The sidebar for the optional Showcase Template\', \'twentyeleven\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\' => "</aside>",
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );

    register_sidebar( array(
        \'name\' => __( \'Footer Area One\', \'twentyeleven\' ),
        \'id\' => \'sidebar-3\',
        \'description\' => __( \'An optional widget area for your site footer\', \'twentyeleven\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\' => "</aside>",
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );

    register_sidebar( array(
        \'name\' => __( \'Footer Area Two\', \'twentyeleven\' ),
        \'id\' => \'sidebar-4\',
        \'description\' => __( \'An optional widget area for your site footer\', \'twentyeleven\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\' => "</aside>",
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );

    register_sidebar( array(
        \'name\' => __( \'Footer Area Three\', \'twentyeleven\' ),
        \'id\' => \'sidebar-5\',
        \'description\' => __( \'An optional widget area for your site footer\', \'twentyeleven\' ),
        \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
        \'after_widget\' => "</aside>",
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );
    }
}
有人能看到这个函数有什么问题吗?

Update: 问题是我真的想改变Main Sidebar 显示器before_widgetafter_widget. 我添加了一个<div>

2 个回复
SO网友:paulruescher

你为什么要重新声明这个函数?

您可以编辑原始函数,或者更好地使用remove_action 删除add_action( \'widgets_init\', \'twentyeleven_widgets_init\' ); 然后重新附加您自己的函数add_action(\'widgets_init\', \'your_widgets_init_function\' );

SO网友:olgri

我也有同样的问题。。。解决方案:http://venutip.com/content/right-way-override-theme-functions但是,请使用“after\\u setup\\u theme”而不是“init”,否则它将不起作用,因为子项起作用。php在父函数之前被调用。php。另一篇有用的文章:http://ottopress.com/2010/wordpress-protip-child-themes/

结束

相关推荐

Functions.php:从博客中排除类别

所以很明显,如何从模板中排除某些类别,但我不想修改4个模板,使它们忽略某个类别。有没有一种方法可以将某个类别从阅读设置的“博客”集中排除?我正在将博客分配到名为“博客”的页面。。。但显然,档案和搜索也需要对这一超出类别的内容视而不见。我宁愿在里面做functions.php