Instructions to create sidebar and get sidebar in wordpress

Tutorials 0 lượt xem

Sidebar in wordpress will make the website more flexible. Help customers can change some elements of the website without having to enter the code.

Similar to the menu in the default wordpress sidebar will not display, we have to enter the code to register as well as display. Today I will show you how to create and display it.

Create sidebar in wordpress

Instructions to create and get sidebar in wordpress

To access the sidebar management, follow the following flow: Go to admin -> Appearance -> widgets

By default, when creating a theme, the sidebar of the website is not displayed. We have to go register the display position of the sidebar.

To register the position of the sidebar display in wordpress, use the following code to insert the functions.php file of the theme you are using.

if (function_exists('register_sidebar')){
    register_sidebar(array(
    'name'=> 'Cột bên',
    'id' => 'sidebar',
));
}
<div><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span></div>

The register_sidebar function is a function used to register a sidebar display position. This function has 2 parameters:

  • Name => Display name of the sidebar in wordpress
  • ID => ID of the sidebar, this id is used to get the sidebar’s data out of the interface

After registering, you go to: Admin -> interface -> widgets will see as shown below is successful.

Add content to the sidebar

A sidebar will contain many widgets. By default, wordpress has provided us with a lot of widgets. The number of widgets here depends on whether your website has installed many widget support plugins or not. In the following article, I will show you how to create a simple widget.

To add content to the widget, I have already written an article on this issue and you can see it here: How to insert widgets into the website .

Get sidebar content in wordpress

To get the content of the sidebar, use the following code to insert it into the position where you need to display the sidebar of the theme you are using.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar') ) : ?><?php endif; ?>
<div><span>1</span></div>

The above code checks if the sidebar is declared, if so, uses the dynamic_sidebar() function to get the sidebar. ‘ sidebar ‘ is the ID of the sidebar declared during sidebar creation.

Epilogue:

Today I have introduced the function to create sidebar as well as get sidebar in wordpress. The content of this article is quite simple.

Mainly, you need to analyze the needs of your customers, then choose the number and location of the sidebar accordingly.

In the following article, I will show you how to write a widget in WordPress. Hello friends! Remember to like the page Learn wordpress to support me.

Bài viết liên quan