首页 问答 正文

如何在WordPress上设置自定义小工具区域?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 123 次

在WordPress中设置自定义小工具区域需要进行以下步骤:

  1. 打开主题的functions.php文件,添加以下代码:
function custom_widget_areas() {
    register_sidebar( array(
        'name' => __( 'Custom Widget Area', 'textdomain' ),
        'id' => 'custom-widget-area',
        'description' => __( 'Add widgets here to appear in your custom widget area.', 'textdomain' ),
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="widget-title">',
        'after_title' => '</h2>',
    ) );
}
add_action( 'widgets_init', 'custom_widget_areas' ); 
  1. 在主题的模板文件中添加代码调用新建的小工具区域:
<?php if ( is_active_sidebar( 'custom-widget-area' ) ) : ?>
    <div class="custom-widget-area">
        <?php dynamic_sidebar( 'custom-widget-area' ); ?>
    </div>
<?php endif; ?> 

上述代码可以在主题的任意模板文件中添加,例如可以在sidebar.php中添加,以便在侧边栏中显示自定义小工具区域。

以上就是在WordPress上设置自定义小工具区域的步骤和示例代码。

大家谈论
    我的见解