函数文档

_wp_block_theme_register_classic_sidebars()

💡 云策文档标注

概述

此函数用于在块主题中注册先前经典主题的侧边栏。它通过检查当前主题是否为块主题,并获取存储的经典侧边栏数据来实现。

关键要点

  • 函数 _wp_block_theme_register_classic_sidebars() 在块主题中注册经典主题的侧边栏。
  • 仅当当前主题是块主题(通过 wp_is_block_theme() 检查)且存在存储的经典侧边栏数据(通过 get_theme_mod('wp_classic_sidebars') 获取)时才执行。
  • 避免使用 register_sidebar() 以防止为主题启用 widgets 支持,而是直接操作 $wp_registered_sidebars 全局变量。

代码示例

function _wp_block_theme_register_classic_sidebars() {
    global $wp_registered_sidebars;

    if ( ! wp_is_block_theme() ) {
        return;
    }

    $classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
    if ( empty( $classic_sidebars ) ) {
        return;
    }

    // Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
    foreach ( $classic_sidebars as $sidebar ) {
        $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
    }
}

📄 原文内容

Registers the previous theme’s sidebars for the block themes.

Source

function _wp_block_theme_register_classic_sidebars() {
	global $wp_registered_sidebars;

	if ( ! wp_is_block_theme() ) {
		return;
	}

	$classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
	if ( empty( $classic_sidebars ) ) {
		return;
	}

	// Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
	foreach ( $classic_sidebars as $sidebar ) {
		$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
	}
}

Changelog

Version Description
6.2.0 Introduced.