register_sidebar()

最后更新于:2021-11-27 21:37:35

register_sidebar( array|string$args=array())

Builds the definition for a single sidebar and returns the ID.

参数

$args

(array|string) (Optional) Array or string of arguments for the sidebar being registered.

  • ‘name’
    (string) The name or title of the sidebar displayed in the Widgets interface. Default ‘Sidebar $instance’.
  • ‘id’
    (string) The unique identifier by which the sidebar will be called. Default ‘sidebar-$instance’.
  • ‘description’
    (string) Description of the sidebar, displayed in the Widgets interface. Default empty string.
  • ‘class’
    (string) Extra CSS class to assign to the sidebar in the Widgets interface.
  • ‘before_widget’
    (string) HTML content to prepend to each widget’s HTML output when assigned to this sidebar. Receives the widget’s ID attribute as %1$s and class name as %2$s. Default is an opening list item element.
  • ‘after_widget’
    (string) HTML content to append to each widget’s HTML output when assigned to this sidebar. Default is a closing list item element.
  • ‘before_title’
    (string) HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element.
  • ‘after_title’
    (string) HTML content to append to the sidebar title when displayed. Default is a closing h2 element.
  • ‘before_sidebar’
    (string) HTML content to prepend to the sidebar when displayed. Receives the $id argument as %1$s and $class as %2$s. Outputs after the ‘dynamic_sidebar_before’ action. Default empty string.
  • ‘after_sidebar’
    (string) HTML content to append to the sidebar when displayed. Outputs before the ‘dynamic_sidebar_after’ action. Default empty string.

Default value: array()

响应

(string) Sidebar ID added to $gc_registered_sidebars global.

源文件

文件: gc-includes/widgets.php

function register_sidebar( $args = array() ) {
	global $gc_registered_sidebars;

	$i = count( $gc_registered_sidebars ) + 1;

	$id_is_empty = empty( $args['id'] );

	$defaults = array(
		/* translators: %d: Sidebar number. */
		'name'           => sprintf( __( 'Sidebar %d' ), $i ),
		'id'             => "sidebar-$i",
		'description'    => '',
		'class'          => '',
		'before_widget'  => '<li id="%1$s" class="widget %2$s">',
		'after_widget'   => "</li>n",
		'before_title'   => '<h2 class="widgettitle">',
		'after_title'    => "</h2>n",
		'before_sidebar' => '',
		'after_sidebar'  => '',
	);

	/**
	 * Filters the sidebar default arguments.
	 *
	 * @since 5.3.0
	 *
	 * @see register_sidebar()
	 *
	 * @param array $defaults The default sidebar arguments.
	 */
	$sidebar = gc_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) );

	if ( $id_is_empty ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */
				__( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ),
				'<code>id</code>',
				$sidebar['name'],
				$sidebar['id']
			),
			'4.2.0'
		);
	}

	$gc_registered_sidebars[ $sidebar['id'] ] = $sidebar;

	add_theme_support( 'widgets' );

	/**
	 * Fires once a sidebar has been registered.
	 *
	 * @since 3.0.0
	 *
	 * @param array $sidebar Parsed arguments for the registered sidebar.
	 */
	do_action( 'register_sidebar', $sidebar );

	return $sidebar['id'];
}
/**
 * Add a sidebar.
 */
function gcdocs_theme_slug_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Main Sidebar', 'textdomain' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'gcdocs_theme_slug_widgets_init' );
/* Better way to add multiple widgets areas */
function widget_registration($name, $id, $description,$beforeWidget, $afterWidget, $beforeTitle, $afterTitle){
	register_sidebar( array(
		'name' => $name,
		'id' => $id,
		'description' => $description,
		'before_widget' => $beforeWidget,
		'after_widget' => $afterWidget,
		'before_title' => $beforeTitle,
		'after_title' => $afterTitle,
	));
}

function multiple_widget_init(){
	widget_registration('Footer widget 1', 'footer-sidebar-1', 'test', '', '', '', '');
	widget_registration('Footer widget 2', 'footer-sidebar-2', 'test', '', '', '', '');
	// ETC...
}

add_action('widgets_init', 'multiple_widget_init');
/* Add Multiple sidebar 
*/
if ( function_exists('register_sidebar') ) {
	$sidebar1 = array(
		'before_widget' => '<div class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',        
		'name'=>__( 'My sidebar 1', 'textdomain' ),	
	);	
	$sidebar2 = array(
		'before_widget' => '<div class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',        
		'name'=>__( 'My sidebar 2', 'textdomain' ),	
	);
    $sidebar3 = array(
		'before_widget' => '<div class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',        
		'name'=>__( 'My sidebar 3', 'textdomain' ),	
	);
    $sidebar4 = array(
		'before_widget' => '<div class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',        
		'name'=>__( 'My sidebar 4', 'textdomain' ),	
	);
	
	register_sidebar($sidebar1);
	register_sidebar($sidebar2);
    register_sidebar($sidebar3);
    register_sidebar($sidebar4);
}