Each website has its own navigation menu, such as the head navigation menu, the bottom navigation menu, the side bar navigation menu, and wordpress website is no exception. So, in the development of WordPress website theme template, how can we add the foreground navigation menu to WordPress website? Well, according to my years of development experience, wordpress mainly provides three ways to create navigation menus for wordpress theme development. These three ways will create different navigation functions. Today, let's take a look at the first way to create navigation menus for wordpress websites - page based navigation menus. Here, we will use the function provided by wordpress - wp_list_pages(), which is a function of wordpress page list.
Let's take a look at the wordpress function, wp_list_pages(), and its structure.
wp_list_pages($defaults);
From the above code, we can see that the wp_list_pages() function has only one parameter, which can be of two types, string type or array type. We will introduce this in the following example. In order to understand the value of this parameter, we will explain this parameter in the form of array.
Parameter introduction:
$defaults=array (depth=>0,//0: display all pages and sub pages by level;//1: display only top-level pages;//2: display level 2 pages;//- 1: display all pages and sub pages by level;
Show_date=>,//Whether to display the creation date
Date_format=>get_option (date_format),//date format
Child_of=>0,//Specify the ID number of the parent page, and display the child pages under the parent page; 0 indicates that all sub pages are displayed;
Exclude=>,//which pages are excluded
Include=>,//which pages are included
Title_li=>Pages,//Whether to display the title of the page list, if not, set it to blank; Set the title as "Pages"
Echo=>1,//Whether to print it to the foreground page. 1 means display, 0 means not display, but only get the value.
Authors=>,//Specify the page created by a specific author
Link_before=>,//the content before the link<a>link_after=>,//the content after the link<a>
Exclude_tree=>,//Exclude parent/child trees
Sort_column=>menu_order,//sorting method, menu_order is set by background; Post_date by publishing time, post_modified by modifying time;
Sort_order=>DESC,//Sort order, ASC order, DESC is reverse order);
As you can see, there are many parameter values of the wp_list_pages() function. In the actual operation of WordPress theme template development, we generally use only a few of them.
Next, we will introduce how the wp_list_pages() function generates the navigation menu based on the page through an example. Let's first take a look at the single page created in the background of the wordpress website, as shown below:
From the above figure, we can see that there are six pages in the background of the wordpress website, of which "submission" is the parent page of "sub page 1" and "sub page 2".
Case 1: We add the following code to the header of the wordpress website template:
$menu=array (depth=>0, title_li=>Page navigation menu, echo=>1,); wp_list_pages($menu);
Let's go to the front page of the wordpress website to see the effect, as shown below:
We can see that the page navigation is displayed, and the sub pages are displayed hierarchically - indented by 2 spaces.
Case 2: Let's modify a parameter code, set the title to null, add a sorting parameter, and modify the hierarchical parameter value. The code is as follows:
$menu=array (depth=>1, title_li=>page navigation menu, echo=>1, sort_order=>DESC, sort_column=>menu_order,); wp_list_pages($menu);
At this time, let's take a look at the effect of the front page of the wordpress website, as shown below:
We can see that the title of the navigation menu has disappeared, and the hierarchy has disappeared, and the sorting has also changed. It is arranged according to the reverse order of page names. There are many parameters of wp_list_pages(), which are simple without one by one demonstration.
Case 3: The parameter of the wp_list_pages() function is of string type.
At the beginning, I said that the parameters of the wp_list_pages() function can be of two types, either string type or array type. We have used the array type in the first two cases. Here, we will introduce the string type again.
Here we take the code of the case to demonstrate how to change the parameters of array type into string type. The code is as follows:
wp_list_pages("depth=1&title=&echo=1&sort_order=DESC&sort_column=menu_order");
In the above code, we use a connector&this special symbol, which is used to connect multiple parameters. The symbol=in the middle need not be explained. It is the equal sign. Through this code, we also achieve the effect of Case 2.
If you want this page based navigation menu to be displayed horizontally at the top, you can modify the code and style of the CSS file of the wordpress website template. I won't say much here.
That's all for this lesson. Those are my views. If you have different views, please comment. At the same time, welcome [like, share, collect] and [follow] me.