Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

WordPress widget new instance creates content duplicates

I don’t see anything wrong with the widget class. The problem you have is that you are displaying the widget area, that contains the widgets, twice in your sidebar.php:

<aside id="secondary" class="widget-area col-sm-12 col-md-12 col-lg-12" role="complementary">
    <div class="col-lg-6 col-md-12">
    <?php dynamic_sidebar( 'main-widget' ); // this will display all the widgets in your main-widget sidebar ?>
</div>
    <div class="col-lg-6 col-md-12">
    <?php dynamic_sidebar( 'main-widget' ); // this will display again all the widgets in your main-widget sidebar ?>
    </div>
</aside><!-- #secondary -->

The second widget in each sidebar display is probably hidden due to some CSS, but I bet it is there, twice, once on the left and one on the right.

So, do not call dynamic_sidebar( 'main-widget' ); twice.

From what I understand you want to wrap the widgets in certain divs so you can put them on two columns. For this you need to filter the before-widget and after-widget params of each widget via the dynamic_sidebar_params filter. Or, better yet you could use some CSS to do the trick.

Here is a starter PHP code that will add wrappers to each widget in your target widget area:

/**
 * @param array $params
 */
function sole_add_widget_columns_wrapper( $params ) {
    // Add the opening wrapper tag
    $params[0]['before_widget'] = PHP_EOL . '<div class="col-lg-6 col-md-12">' . PHP_EOL . $params[0]['before_widget'];

    // Add the closing wrapper tag
    $params[0]['after_widget'] = $params[0]['after_widget'] . PHP_EOL . '</div><!-- close wrapper -->' . PHP_EOL;
}

/**
 * @param string $index
 */
function sole_handle_main_widget_area_columns( $index ) {
    // We only want to deal with the main widget area
    if ( 'main-widget' !== $index ) {
        return;
    }

    // Filter each widget params and add the wrappers
    add_filter( 'dynamic_sidebar_params', 'sole_add_widget_columns_wrapper', 10, 1 );

    // Hook an action to remove the filter above after we are done with this widget area.
    // This way we don't affect other widget area that may come, on the same page, after this one.
    add_action( 'dynamic_sidebar_after', 'sole_remove_main_widget_area_columns_filter', 10 );
}
add_action( 'dynamic_sidebar_before', 'sole_handle_main_widget_area_columns', 10 );

function sole_remove_main_widget_area_columns_filter() {
    remove_filter( 'dynamic_sidebar_params', 'sole_add_widget_columns_wrapper', 10 );
}

This code will wrap each widget, it will not put even and odd widgets into two column wrappers.

Related Posts:

  1. Where is the content of widgets stored in mysql table
  2. Get sidebar parameters (before_widget, before_title, etc.) from within a widget
  3. Add div class to only one widget
  4. How to avoid widgets added to sidebar on theme activation?
  5. More flexible sidebar and widget management
  6. duplicate sidebar
  7. Removing custom widget area WordPress 4.4 [closed]
  8. Insert Widget option into mark-up with register_sidebar
  9. Cannot save widgets in custom sidebars
  10. Can’t see widget areas in my customizer
  11. Widgets not activated alert after using get_sidebar()
  12. Can you target a widget_class in a register_sidebar?
  13. Copy Widget Settings because of changed IDs
  14. Add class to on sidebar widget
  15. Why is registering a sidebar for each page causing my sidebars to reset?
  16. activate custom sidebar widgets
  17. How to create sidebar from “scratch”?
  18. How to get sidebar widgets in leftsidebar template
  19. $before/after_widget/title not displaying anything
  20. Unique widget id in sidebar
  21. Programmatically add widgets to sidebars
  22. Loading scripts only if a particular shortcode or widget is present
  23. Limit number of Widgets in Sidebars
  24. Give Editor Access To Sidebar
  25. Translate widget titles using qTranslate plugin
  26. Get number of widgets in sidebar
  27. Text Widget creates a
  28. How Do I Add Custom CSS To Only Certain Widgets
  29. Add the sidebar/widget editor to the post edit screen?
  30. Show Woocommerce minicart widget in checkout page sidebar? And, how to make this update secure by overriding widget?
  31. HowTo: Add Class to Sidebar Widget List-Items
  32. Different widgets on different page templates?
  33. Widgets not displaying in theme customizer
  34. $post>ID displays wrong post ID
  35. Register multiple sidebars
  36. How to add author details in the post sidebar?
  37. Determining a Widget Instance and Sidebar Location?
  38. Randomize widgets displayed in my sidebar [duplicate]
  39. Video Embed in Sidebar Widget with Links to Others in Category
  40. Remove All Widgets from Sidebar
  41. Unregistering a Sidebar in Child Theme
  42. What is the use case for the “Class” parameter in register_sidebar?
  43. Add a select box to all widgets
  44. Problems with the sidebar args and wp_list_bookmarks
  45. Ban certain widgets from certain sidebars
  46. Registering Sidebars and Sidebar Widgets. Sidebar Widgets Not Displaying
  47. Is It Possible to Restore Accidentally Deleted Widgets?
  48. Manual display of widget
  49. Sidebar Widget Registration without a name, how is it assigned to new named sidebar widget?
  50. Hooking Into Widget Output Loop
  51. is_active_sidebar() Always Returns False
  52. Call sidebar from a template
  53. How could a Widget behave differently depending on sidebar
  54. Adding classes to dynamic sidebar
  55. Is there a way to allow only certain type of widgets in the sidebars?
  56. Display sidebar that created in functions.php
  57. Create variable from widget instance
  58. Why do none of my widgets have a title?
  59. Save/update widget outside admin panel [closed]
  60. Widget area inside a widget
  61. WordPress: Apply filter/hook to a particular sidebar widgets?
  62. How to insert widget areas specific to certain pages (or posts, etc.)?
  63. List sidebars on a page
  64. Why use dynamic_sidebar() conditionally?
  65. wordpress widget textbox in the sidebar
  66. WordPress Widget multiple use
  67. Contact Form in sidebar [closed]
  68. dynamic_sidebar() returns false in admin section
  69. How to check if searchform.php is being included as widget?
  70. the_widget() and widget’s ID
  71. How to get sidebar’s content inside admin-ajax?
  72. Full width layout for custom post type pages
  73. How to “pair” two widgets side by side in one sidebar [closed]
  74. Widgets not expanding on wordpress 3.3 widget dashbord
  75. Adding custom html and standard widgets to sidebar
  76. Dynamic Sidebars On Multiple Subpages
  77. How to add new sidebar widget area to child theme?
  78. Check which registered sidebar a widget is added to
  79. Disable widgets on specific posts
  80. WordPress widget/sidebar dividers?
  81. adding multiple sidebars on pages
  82. Display the id list of active widgets of same sidebar?
  83. How to remove a widget from the sidebar on specific page?
  84. wp_register_sidebar_widget() disappered my Widgets submenu – what am I doing wrong?
  85. Add individual tag to widget title in sidebar
  86. Toggle option in sidebar widgets
  87. Excluding specific widgets from default sidebar class
  88. Can WordPress Read Its Own RSS Feed?
  89. different class (css) for sidebar widgets
  90. Dynamic sidebar based on category
  91. Custom page sidebar using Template dropdown box
  92. How to show different widgets on different pages in a user friendly way
  93. Show widget differently depending on if it’s in the sidebar or footer
  94. How can I assign widgets from WP to appear in bbpress?
  95. How can I get the first section of a page 100% width, while the rest will have a sidebar? [closed]
  96. Trying to create dynamic widget area using post ID
  97. How to create pagination for homepage with widgetized area?
  98. Dynamically add content to an existing widget area
  99. How can I inject html after the [x]th widget inside a Sidebar? [duplicate]
  100. Default widgets assignment – isn’t working
Categories widgets Tags mysql, post-meta, register-sidebar, sidebar, widgets
Author functions don’t work in customizer’s selective refresh
Can’t upload media to my Raspberry Pi WordPress server

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress