Home  >  Article  >  Backend Development  >  Explanation of PHP functions used to create and obtain sidebars in WordPress, wordpressphp_PHP tutorial

Explanation of PHP functions used to create and obtain sidebars in WordPress, wordpressphp_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:02874browse

Explanation of PHP functions used to create and obtain sidebars in WordPress, wordpressphp

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 );

Parameters

$args

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

Default value:

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

Introduction to array parameters:

  • name: sidebar name
  • id: sidebar ID, must be lowercase, default is an increasing array ID
  • description: Sidebar description
  • class: additional class for the gadgets in it
  • before_widget: the beginning Html code of the widget inside
  • after_widget: Html code at the end of the widget inside
  • before_title: Html code at the beginning of the title of the gadget inside
  • after_title: Html code at the end of the title of the gadget inside

Example

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

Others

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

get_sidebar() (get 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) The name of the template to import. If specified, the sidebar-{$name}.php file in the current theme root directory will be imported.

Default value: None

Example

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

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

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

<&#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 at: wp-includes/general-template.php

Articles you may be interested in:

  • Tips for customizing the color scheme of the backend management interface in WordPress
  • Analysis of related functions for sending http requests in WordPress
  • How to set default content in the WordPress post editor
  • Detailed explanation of how to use functions to add and perform actions in WordPress

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1086653.htmlTechArticleExplanation of PHP functions used to create and obtain sidebars in WordPress, wordpressphp register_sidebar() (create sidebar ) Create a sidebar to place widgets. This function makes...
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