Monday, August 18, 2014

How to add a custom widget area in WordPress

First you’ll want to add this code to your active theme’s function.php file. This code is used to register the custom widget area and create it on the backend.





// Custom widget area.
 register_sidebar( array(
    'name' => __( 'Custom Widget Area'),
    'id' => 'custom-widget-area',
    'description' => __( 'An optional custom widget area for your site', 'twentyten' ),
    'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
    'after_widget' => "</li>",
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

In above code you can give your own id and own name.
 
While creating multiple widget area you should keep the id different for each one.


Next you want to add this code to whichever page template you’d like the widget area to show on. This code is used to display the custom widget area in any location you choose within your theme’s files.






<?php
// Custom widget area start
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Custom Widget Area') ) : ?>
<?php endif; ?>

No comments:

Post a Comment