A Comprehensive Guide On Calling Pages Within Menus Dynamically

Total
0
Shares
Guide On Calling Pages Within Menus Dynamically

As a web designer, you might have created different types of websites; the ones which are based on web-pages and others which are based on blog sections. If you’re one who’s been building the former type of websites using WordPress then this is a post just for you.

What’s In Store For You In This Post?

This is a quick tutorial that allows you to learn the process of calling pages within menus dynamically- a great source of help for those of you who find it challenging to call pages within menus.

Don’t Forget Calling Pages Dynamically From The WP Back-End

Calling pages (for example: About Us, Contact Us etc.) dynamically from the WP back-end wouldn’t expect you to write lengthy code. All you need to do is simply add a simple code snippet.

Now, here’s an example showcasing the process of calling pages within menus dynamically:

WordPress is equipped with an inbuilt function called wp_list_pages($args) which is used for displaying a list of all pages and sub-pages included within the WP page-based website. Additionally, this function has an array type parameter that’s used for managing the structure of pages and their listing. The different parameters available for $args include the following:

$args = array(

'authors'     => '',
'child_of'     => 0,
'date_format' => get_option('date_format'),
'depth'       => 0,
'echo'         => 1,
'exclude'     => '',
'include'     => '',
'link_after'   => '',
'link_before' => '',
'post_type'   => 'page',
'post_status' => 'publish',
'show_date'   => '',
'sort_column' => 'menu_order, post_title',
'sort_order'   => '',
'title_li'     => __('Pages'),
'walker'       => ''

);

Explanation of $args available in wp_list_pages

Here’s a detailed explanation of all function arguments that have been covered in the above snippet:

1. authors:

argument type==>(string)

As per this argument, you can opt for including just the pages which have been authored by authors using a comma-separated list of author IDs.

2. child_of:

argument type==>(integer)

With this argument, you can choose to display all the sub-pages included for a single page. The page’s ID is used as a value. Also, the child_of parameter with derive the “grandchildren” of the specified page ID instead of the immediate children. This argument has a default value of 0. That means all pages and sub-pages are displayed.

3. date_format:

argument type ==> (string)

This argument is being used for controlling the format of Page date that’s set by show_date parameter(the value for which can be I, J, F, Y) and defaults to date format which has already been configured using WordPress settings options.

4. depth:

argument type==>(integer)

With the above argument, you can easily control the count for levels in the page hierarchy which needs to go into the list that has been generated by wp_list_pages. The default value for wp__list_pages is 0, meaning that all pages and their related sub-pages are displayed at any depth, the value -1 means that all pages will be displayed at any depth and arranged in a flat list, the value 1 means that only top-level pages will be displayed and finally the value 2, 3…..and son on means that pages will be displayed at a given depth.

5. echo:

argument type==?(boolean)

You can use this argument for toggling the display of generated list of links or for returning the page list in the form of an HTML text string that can be used in PHP. Values which can be assigned to this argument include 1(True) and 0(False). 1(True) being the default value will display the generated list items.

6. short_order:

argument type==> (string)

Use this argument for changing the sorting order for pages, either in an ascending order or descending order. Values for this argument can be any one of the below two:

  • ‘ASC’- This will sort the page list from lowest to highest. It is the default value for this argument.
  • ‘DESC’- This will sort the page list from highest to lowest

7. exclude:

argument type ==> (string)

Use this argument for defining a comma-separated list of pages IDs which need to be executed from the main page list. For example, exclude=3, 7, 23….. No default value is assigned to this argument.

8. exclude_tree:

argument type ==> (string)

Use this argument for defining a comma-separated list of parent page IDs which need to be executed from page list. In other words, you can use this argument for excluding a particular parent page id and all child pages available for a specific parent page. For instance, ‘exclude_tree=7’ will exclude the parent page 7 and all its related child pages.

argument type ==> (string)

This argument will be used for setting text or HTML which will precede the link text that’s included within the <a> tag.

argument type ==> (string)

This argument will be used for setting the text or HTML which will follow the link text that’s available within the <a> tag.

11. sort_column:

argument type ==> (string)

This is an argument which will sort all page lists in different ways with the default settings being sorted in an alphabetical manner using the Page title. Valid values for this argument include the following:

  • ‘post_title’ – Use this value to sort Pages alphabetically (by title). This is the default value assigned to the argument.
  • ‘menu_order’ – Use this value to sort Pages by Page Order. Do note the difference between Page Order and Page ID.
  • ‘post_date’ – Use this value to sort pages by their creation time.
  • ‘post_modified’- Use this value to sort pages by last modified time.
  • ‘ID’ – Use this value to sort pages by numeric Page ID.
  • ‘post_author’ – Use this value to sort pages by the Page author’s numeric ID.
  • ‘post_name’ – Use this value to sort pages in an alphabetical format using Post slug.

12. sort_order:

argument type ==> (string)

You can use this argument for changing the sorting order for the list of pages, either in an ascending or descending order. Values that can be assigned to this argument include the following:

  • ‘ASC’- This value will sort the pages from lowest to highest.
  • ‘DESC’- This value will sort the pages from highest to lowest.

13. Walker:

Walker is basically an abstract class which “walks” through each individual node available within a tree and executes an abstract function at each node. Here, the tree can either be an object or an associative array. You can opt for defining the abstract methods within the custom child class in order to take an action at every node.

14. post_type:

argument type ==> (string)

The above argument will list all the posts which are related to a specific hierarchical Post Type. You can choose to assign any of the following values to this argument:

  • ‘page’
  • ‘revision’
  • Hierarchical Custom Post Types

15. post_status:

argument type ==> (string)

Use the above argument for displaying a comma-separated list of post status types you want to return. Values for this argument can be any one of the following:

  • Publish
  • Private
  • Draft

Additionally, you can use the below code snippet for displaying pages and their sub-pages:

<?php
$args = array(  'depth'       => -1
);
wp_list_pages($args);
?>

With that, we’re done!

Conclusion

Hope you’d have found the above tutorial handy enough for calling pages dynamically for your WordPress site. If you have any queries regarding the details covered above, please don’t hesitate in using the comments box provided right under this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like

WordPress as a Twitter-style Blog

Microblogging is a form of blogging. A microblog differs from a traditional blog in that its content is typically much smaller, in both actual size and aggregate file size. WordPress…