问题
I want to create custom right sidebar for my wordpress site.
Something like this wordpress_right_sidebar
- It should be totally responsible.
- I'm going to post more items.
- What are the things i gotta need?
回答1:
Add this code into functions.php in order to register your custom sidebar:
function my_custom_sidebar() {
register_sidebar(
array (
'name' => __( 'Custom', 'your-theme-domain' ),
'id' => 'custom-side-bar',
'description' => __( 'Custom Sidebar', 'your-theme-domain' ),
'before_widget' => '<div class="widget-content">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
}
add_action( 'widgets_init', 'my_custom_sidebar' );
I want to render the custom sidebar in single posts only, so I'll edit the "Single post" file in single.php
<?php if ( is_active_sidebar( 'custom-side-bar' ) ) : ?>
<?php dynamic_sidebar( 'custom-side-bar' ); ?>
<?php endif; ?>
- Go to Appearance > Widgets to see if the new sidebar available.
- Add the widgets you need.
来源:https://stackoverflow.com/questions/43932377/creating-custom-sidebar-wordpress