Home >Backend Development >PHP Tutorial >An explanation of the PHP functions used to create and obtain sidebars in WordPress

An explanation of the PHP functions used to create and obtain sidebars in WordPress

WBOY
WBOYOriginal
2016-07-29 09:08:22968browse

register_sidebar() (create sidebar)
Create a sidebar to place widgets. When using this function, please put it in a function and mount it to the "widgets_init" hook.

Usage

register_sidebar( $args );

Arguments

$args

(String | Array) (Optional) Arguments for the sidebar to create.

Default value:

$args = array(
  'name'     => __( 'Sidebar name', 'theme_text_domain' ),
  'id'      => 'unique-sidebar-id',
  'description'  => '',
  'class'     => '',
  'before_widget' => '<li>',
  'after_widget' => '</li>',
  'before_title' => '<h2>',
  'after_title'  => '</h2>'
);

Array parameter introduction:

  • name: sidebar name
  • id: sidebar ID, must be lowercase, default is an increasing array ID
  • description: sidebar description
  • class: Extra class for the widget inside
  • before_widget: The beginning Html code of the widget inside
  • after_widget: The Html code at the end of the widget inside
  • before_title: The beginning Html of the title of the widget inside Code
  • after_title: Html code at the end of the title of the widget

Example

register_sidebar( array(
  'name'     => __( '右边的侧边栏' ),
  'id'      => 'sidebar-1',
  'description' => __( '右侧边栏的小工具。' ),
  'before_title' => '<h3>',
  'after_title' => '</h3>',
));

Others

The function is located at: wp-includes/widgets.php

get_sidebar() (get the sidebar)
get_sidebar() is used to introduce the sidebar template. If the name is specified, the sidebar-{name}.php file in the current theme root directory will be imported. If not specified, the sidebar.php file in the current theme root directory will be imported. If the file does not exist, wp-includes/theme-compat/sidebar.php will be imported. document.

Usage

get_sidebar( $name );

Parameters

$name

(String) (optional) Introduces the name of the template. If specified, the sidebar-{$name}.php file in the current theme root directory will be introduced.

Default value: None

Example

The code below will import the sidebar.php file in the root directory of the current theme:

<&#63;php get_sidebar(); &#63;>

The code below will import the sidebar-left.php file in the root directory of the current theme:

<&#63;php get_sidebar( 'left' ); &#63;>

The following example introduces the left sidebar (sidebar-left.php) and the right sidebar (sidebar-right.php) respectively:

<?php get_header(); ?>
<&#63;php get_sidebar( 'left' ); &#63;>

Content content

<&#63;php get_sidebar( 'right' ); &#63;>
<&#63;php get_footer(); &#63;>

Others

This function is located :wp-includes/general-template.php

The above is an introduction to the PHP functions used to create and obtain sidebars in WordPress, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Share PHP daemon classNext article:Share PHP daemon class