简体   繁体   中英

How to add wordpress widget widget to static Sidebar via code?

I'm making a WordPress site, and have a static sidebar, and I was wondering how I would add a plugin widget to said sidebar. Here is my sidebar code:

<div id="sidebar">
    <div class="sidebar-entry sidebar-both">    
        <ul>
             <?php the_widget( 'WP_Widget_Search' ); ?>  
        </ul>
    </div>

    <div class="sidebar-entry sidebar-left">    
        <h2> <?php _e('Categoies'); ?></h2>
        <ul>
            <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
        </ul>
    </div>
    <div class="sidebar-entry sidebar-right">   
        <h2> <?php _e('Archives'); ?></h2>
        <ul>
            <?php wp_get_archives('type=monthly'); ?>
        </ul>
    </div>

    <div class="sidebar-entry sidebar-right">   
        <ul>
            <?php the_widget( 'WP_Widget_Meta' ); ?>  
        </ul>
    </div>
</div>

first you can make a widget area in your theme by adding this code in your functions.php file

if ( function_exists('register_sidebar') )
register_sidebar(array(
    'name'=> 'Custom Sidebar',
    'id' => 'custom_sidebar',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

now you have widget area 'Custom Sidebar' in appearance->widget

now just drag any widget in this area and to access this in your custom sidebar, call dynamic_sidebar() with id as:

<?php dynamic_sidebar('custom_sidebar'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM