The list_pages WordPress PHP Filter allows you to modify the page title when creating an HTML drop-down list of pages.
Usage
add_filter('list_pages', 'your_custom_function', 10, 2);
function your_custom_function($title, $page) {
// your custom code here
return $title;
}
Parameters
- $title (string) – The page title.
- $page (WP_Post) – The page data object.
More information
See WordPress Developer Resources: list_pages
Examples
Append Page ID to Page Title
This example appends the Page ID to the page title in the drop-down list.
add_filter('list_pages', 'append_page_id_to_title', 10, 2);
function append_page_id_to_title($title, $page) {
$title .= ' (' . $page->ID . ')';
return $title;
}
Add Post Status to Page Title
This example adds the post status to the page title in the drop-down list.
add_filter('list_pages', 'add_post_status_to_title', 10, 2);
function add_post_status_to_title($title, $page) {
$title .= ' [' . $page->post_status . ']';
return $title;
}
Make Draft Page Titles Italic
This example makes the titles of draft pages italic in the drop-down list.
add_filter('list_pages', 'italicize_draft_titles', 10, 2);
function italicize_draft_titles($title, $page) {
if ($page->post_status == 'draft') {
$title = '<i>' . $title . '</i>';
}
return $title;
}
Remove “Private:” Prefix from Private Pages
This example removes the “Private:” prefix from private pages in the drop-down list.
add_filter('list_pages', 'remove_private_prefix', 10, 2);
function remove_private_prefix($title, $page) {
$private_title_format = __('Private: %s');
$title = str_replace(sprintf($private_title_format, ''), '', $title);
return $title;
}
Add Custom Prefix to Page Titles Based on Template
This example adds a custom prefix to the page titles based on the page template in the drop-down list.
add_filter('list_pages', 'add_custom_prefix_based_on_template', 10, 2);
function add_custom_prefix_based_on_template($title, $page) {
$template = get_page_template_slug($page->ID);
if ($template == 'template-about.php') {
$title = '[About] ' . $title;
} elseif ($template == 'template-contact.php') {
$title = '[Contact] ' . $title;
}
return $title;
}