Para crear un sidebar en Worpdress lo primero que haremos es añadir al archivo functions.php el siguiente código. En el siguiente ejemplo se crean dos sidebars (izquierdo y derecho):
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Left Sidebar',
'id' => 'left-sidebar',
'description' => 'Barra lateral izquierda',
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Right Sidebar',
'id' => 'right-sidebar',
'description' => 'Barra lateral derecha',
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
Para mostrarlos añadiremos allá donde queramos (page, post, etc…) el siguiente código. En este ejemplo he utilizado el archivo page.php relativo a las páginas estáticas de WP:
<?php get_header(); ?>
<section id="content" role="main">
<div id="left-sidebar">
<ul>
o<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left-sidebar') ) :
endif; ?>
</ul>
<div style="clear:both"></div>
</div>
<div id="escenario"><?php echo apply_filters('the_content', $post->post_content); ?></div>
<div id="right-sidebar">
<ul>
o<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right-sidebar') ) :
endif; ?>
</ul>
<div style="clear:both"></div>
</div>
</section>
<?php get_footer(); ?>
Con estos dos simples pasos ya tenemos los sidebars activos en el panel de control (Apariencia/Widgets) listos para recibir widgets.
