简体   繁体   中英

How to create and active Left sidebar on Wordpress theme?

I can't create left sidebar.

I created a file into child theme folder: sidebar-left.php:

<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
  <aside class="sidebar">            
  <!-- SIDEBAR WIDGET AREA -->
    <?php if ( is_active_sidebar( 'lhsidebar' ) ) : ?>
      <?php dynamic_sidebar( 'lhsidebar' ); ?>
      <?php else : ?>
        <p><?php esc_html_e('No widgets added', 'rehub-theme'); ?></p>
      <?php endif; ?>        
   </aside>

Then, I have added this code into child theme's function.php :

// Left SideBar
register_sidebar( array(
        'name'          => esc_html__( 'Left Sidebar', 'rehub' ),
        'id'            => 'lhsidebar',
        'description'   => esc_html__( 'Add widgets here.', 'rehub' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
) );

But where should I put the get_sidebar('sidebar-left') ?

You have to call the sidebar inside any template of your child theme either by using the get_sidebar() function:

<?php get_sidebar('left'); ?>

Note: the name of the file without sidebar- part at the begin of the name as mentionned in the doc .

Either by using the get_template_part() function if your template file is not in the same directory as the sidebar file.

<?php get_template_part( 'sidebar-templates/sidebar', 'left' ); ?>

Note: here the full name of the sidebar is mentionned in the function, as well as its path from the root of the child-theme, cf doc .

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