Shivam Shukla
Shivam Shukla

Reputation: 91

How to add widget area in wordpress

I want toadd a widget area in my main section where recent posts were display. But i can't be able to do. Here what i wanted to do. enter image description here

So the red rectangle widget area i want to add. But every time i insert a code of adding widget to my function.php file it's add an widget area in sidebar. Please friends anyone help me how to do this.

Upvotes: 3

Views: 3221

Answers (2)

It's simple.

1. Add the widget display code to the theme files.

locate the part where you want to show the widget and add the following code

if ( function_exists('dynamic_sidebar')) {
   dynamic_sidebar( 'id-of-the-widget-area' );
}

This will display your widget area in the area where you added the code.

2. You can also display the widget through plugin

if there is a hook in the theme file, and you know that specific hook. Then adding the following code to your plugin file will display the specific widget

add_action('replace_with_specific_hook_in_theme', 'my_custom_widget_area');
function my_custom_widget_area() {

    // Optional: Check if you are on the specific page

    if ( function_exists('dynamic_sidebar')) {
       dynamic_sidebar( 'id-of-the-widget-area' );
    }

}

Upvotes: 0

Andy Tschiersch
Andy Tschiersch

Reputation: 3815

1. Define a new widget area with register_sidebar()

2. Generate widget ouput with dynamic_sidebar()

Note: This must not be inside of a sidebar, you can place the widget output where you want.

Read Widgetizing Themes for more informations and examples.


Example:

Create a new widgetarea: (place it in your functions.php)

function my_widgets_init() {

    register_sidebar( array(
        'id'            => 'above-recent-posts',
        'name'          => 'Widgets above recent posts',
    ) );

}
add_action( 'widgets_init', 'my_widgets_init' );

Output widgets: (place this where you want the widgets should appear)

dynamic_sidebar( 'above-recent-posts' );

Upvotes: 3

Related Questions