wp_assign_widget_to_sidebar

函式
wp_assign_widget_to_sidebar ( $widget_id, $sidebar_id )
引數
  • (string) $widget_id The widget ID to assign.
    Required:
  • (string) $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar.
    Required:
定義位置
相關方法
wp_find_widgets_sidebarwp_get_sidebarwp_parse_widget_idwp_list_widget_controls_dynamic_sidebarwp_ajax_widgets_order
引入
5.8.0
棄用
-

wp_assign_widget_to_sidebar: 這個函式用來把一個widget分配到WordPress的一個特定的側邊欄。它需要兩個引數–小工具例項和小工具將被分配的側邊欄的ID: 這個函式也將新的小工具配置儲存到資料庫中。

為給定的側邊欄分配一個小工具。

function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
	$sidebars = wp_get_sidebars_widgets();

	foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
		foreach ( $widgets as $i => $maybe_widget_id ) {
			if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
				unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
				// We could technically break 2 here, but continue looping in case the ID is duplicated.
				continue 2;
			}
		}
	}

	if ( $sidebar_id ) {
		$sidebars[ $sidebar_id ][] = $widget_id;
	}

	wp_set_sidebars_widgets( $sidebars );
}

常見問題

FAQs
檢視更多 >