繁体   English   中英

如何在 Wordpress 主题上创建和激活左侧边栏?

[英]How to create and active Left sidebar on Wordpress theme?

我无法创建左侧边栏。

我在子主题文件夹中创建了一个文件: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>

然后,我将此代码添加到子主题的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>',
) );

但是我应该把get_sidebar('sidebar-left')放在哪里?

您必须使用get_sidebar()函数在子主题的任何模板中调用侧边栏:

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

注意:文件的名称在名称的开头没有sidebar-部分,如文档中所述。

如果您的模板文件与侧边栏文件不在同一目录中,则使用get_template_part()函数。

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

注意:这里在函数中提到了侧边栏的全名,以及它从子主题的根开始的路径,cf doc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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