Are you looking for an effective way to create multiple widgets in WordPress without writing too much code?
I will share with you a swift solution to create multiple widgets in WordPress. This will not only reduce your time but you will reuse the trick in multiple projects especially if you are a professional WordPress developer.
This is the greatest approach to creating multiple widgets in WordPress though the second option can also come in handy when you want multiple similar widgets. You can check the second method in the blog How To Registering A Sidebar And Use In Your Custom Theme In WordPress.
For example, when you want to create multiple footer widgets, it can be easier to use the for loop as follows:
Please copy the below code and paste it into your child theme’s functions.php file.
add_action( 'widgets_init', 'register_sidebars_dynamically' ); function register_sidebars_dynamically() { for ( $i = 1, $n = 5; $i <= $n; $i++ ) { register_sidebar( array( 'name' => esc_html__( 'Footer Area #', 'thecodehubs' ) . $i, 'id' => 'footer-' . $i, 'description' => sprintf( esc_html__( 'The #%s column in footer area', 'thecodehubs' ), $i ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } }
I think this can be one of the most functional solutions to creating multiple widgets in WordPress since the for loop simply counts to 5 and creates 5 widgets you can just swap the number to any other number of widgets you want and the multiple widgets will be generated dynamically.
For the output, Please check the below video.