[Resolved] 3 new widgets (page-new.php)

Home Forums Support [Resolved] 3 new widgets (page-new.php)

Home Forums Support 3 new widgets (page-new.php)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #55065
    Jean Paiva
    Developer

    Hey Tom, I’m stuck in something here. (I’m using child theme)

    I need to create 3 widgets area after header so I created a new page (page-homepage.php) and I call the new widgets:

    <div class="grid-container">
    		<div class="grid-33"><?php dynamic_sidebar( 'left-top' ); ?></div>
    		<div class="grid-33"><?php dynamic_sidebar( 'mid-top' ); ?></div>
    		<div class="grid-33"><?php dynamic_sidebar( 'right-top' ); ?></div>
    	</div>

    And my functions.php I used this code:

    <?php
    /**
     * Generate child theme functions and definitions
     *
     * @package Generate
     */
     
     add_action( 'widgets_init', 'generate_widgets_init' );
    	function generate_widgets_init() {
    		register_sidebar( array(
    			'name'          => __( 'Left Top', 'generate' ),
    			'id'            => 'left-top',
    			'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    			'after_widget'  => '</aside>',
    			'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    			'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    		) );
    	function generate_widgets_init() {
    		register_sidebar( array(
    			'name'          => __( 'Mid Top', 'generate' ),
    			'id'            => 'mid-top',
    			'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    			'after_widget'  => '</aside>',
    			'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    			'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    		) );
    	function generate_widgets_init() {
    		register_sidebar( array(
    			'name'          => __( 'Right Top', 'generate' ),
    			'id'            => 'right-top',
    			'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    			'after_widget'  => '</aside>',
    			'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    			'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    		) );
    
    }
    ?>

    The problem is that I’m getting syntax error on my functions.php (line 38), do you know what can I do for fix that?

    #55128
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You’re using the same function name as the parent theme, and you’re calling it 3 times.

    PHP doesn’t allow for the same function names to run as it doesn’t know which one to read.

    Try this:

    <?php
    /**
     * Generate child theme functions and definitions
     *
     * @package Generate
     */
     
    add_action( 'widgets_init', 'generate_custom_widgets_init' );
    function generate_custom_widgets_init() {
    	register_sidebar( array(
    		'name'          => __( 'Left Top', 'generate' ),
    		'id'            => 'left-top',
    		'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    		'after_widget'  => '</aside>',
    		'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    		'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    	) );
    
    	register_sidebar( array(
    		'name'          => __( 'Mid Top', 'generate' ),
    		'id'            => 'mid-top',
    		'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    		'after_widget'  => '</aside>',
    		'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    		'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    	) );
    
    	register_sidebar( array(
    		'name'          => __( 'Right Top', 'generate' ),
    		'id'            => 'right-top',
    		'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
    		'after_widget'  => '</aside>',
    		'before_title'  => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">' ),
    		'after_title'   => apply_filters( 'generate_end_widget_title', '</h4>' ),
    	) );
    
    }
    #55129
    Jean Paiva
    Developer

    Now the error it’s:

    Cannot redeclare generate_widgets_init()

    Should use “remove_action” first? Any clue?

    I found this topic https://wordpress.org/support/topic/override-twentyeleven-functionsphp-using-child-theme

    maybe I should try to do that?

    #55130
    Tom
    Lead Developer
    Lead Developer

    Sorry about that – try the code now ๐Ÿ™‚

    #55131
    Jean Paiva
    Developer

    Yes, that is it! Thank you so much for your support!
    Your theme is just awesome!

    You just changed the function name?

    #55132
    Tom
    Lead Developer
    Lead Developer

    Yea – generate_widget_init is called in the core theme, and you can’t call the same function twice ๐Ÿ™‚

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.