wp_list_pages

Home » Hooks » wp_list_pages

Hook Name: wp_list_pages

In WordPress, the wp_list_pages hook is a powerful tool that allows developers to modify the output of the wp_list_pages function. This function is commonly used to create a dynamic list of pages on a WordPress website.

The wp_list_pages function generates an HTML unordered list (ul) of all the published pages on a WordPress site, providing a convenient way to display a navigation menu for site visitors. This function uses a default set of parameters to determine which pages to include in the list, but with the help of the wp_list_pages hook, developers can customize the output as per their specific requirements.

By utilizing the wp_list_pages hook, developers can modify the output HTML, add custom CSS classes to the list items, modify the link attributes, or even exclude specific pages from being displayed. This hook empowers developers to have full control over the appearance and behavior of the page list, enhancing the overall user experience.

Example Usage:

function custom_wp_list_pages($output, $args) {
    // Modify the HTML output to add a custom class to the list items
    $output = str_replace('<li', '<li class="custom-class"', $output);

    // Exclude a specific page with ID 42 from being displayed
    $output = str_replace('<li class="custom-class"><a href="http://example.com/?p=42"', '<li class="custom-class" style="display:none;"><a href="http://example.com/?p=42"', $output);

    // Return the modified output
    return $output;
}
add_filter('wp_list_pages', 'custom_wp_list_pages', 10, 2);

In this example, we have created a custom function custom_wp_list_pages and attached it to the wp_list_pages hook using the add_filter function. Inside the custom function, we modify the HTML output by adding a custom CSS class to the list items and excluding a specific page with ID 42 from being displayed.

By using the wp_list_pages hook and customizing the function’s output, developers can seamlessly integrate the generated page list with their website’s design and functionality requirements.

Learn More on WordPress.org

WordPress snippets using the wp_list_pages hook

Register an account to save your snippets or go Pro to get more features.