簡體   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