wp_list_pages, include parameter?
This came from a post on Wordpress Support where the member was looking to lessen his workload by ‘including’ the pages he wanted to display rather than excluding them. As he posts new pages regularly he had to constantly update the pages to ‘exclude’ in his sidebar navigation. Here is the post:
Hello,
This is my first website with wordpress so please excuse if my question is obvious.
I want to create a navigation menu on the sidebar using wp_list_pages funtion, but instead of excluding certain pages I want to INCLUDE only certain pages. Is this possible?
The reason for this is that as I add more pages to the site I constantly need to update my sidebar.php to exclude the new page ids.
I thought of the possibility of creating a static menu linking directly to the pages in question, but then I loose the current_page_item class that is otherwise automatically added.
Any pointers would be of great help.
Thanks in advance,
Frank
In response to Frank’s query, Otto42 came up with a little known parameter that can be used in the ‘wp_list_pages’: the ‘include’ parameter.
Though it's not documented well, wp_list_pages() appears to support the "include" parameter. So you can do something like this:
wp_list_pages('include=4,8,15,16,23,42');
To show only those pages. Not entirely sure how that would work, mind you, so the results may be unusual. Or they may not. Hard to say.
This does work! but I like to keep on my toes and this seemed like a simple little thing to accomplish, so I took a stab at it.
<?php
function get_the_pa_ges (){
global $wpdb;
if ( ! $these_pages = wp_cache_get('these_pages', 'pages') ) {
$these_pages = $wpdb->get_results('select ID, post_title from '. $wpdb->posts .' where post_status = "publish" and post_type = "page" order by ID');
wp_cache_add('these_pages', $these_pages, 'pages');
}
return $these_pages;
}
function list_certain_pages($page_ids=''){
$page_ids = explode(',',$page_ids);
$output = '';
$these_pages = get_the_pa_ges ();
foreach ($these_pages as $thats_them){
$the_page_id = $thats_them->ID;
if (isset($page_ids) && in_array($the_page_id,$page_ids)){
$output .= '<a href="'.get_permalink($thats_them->ID).'" title="'.$thats_them->post_title.'">'.$thats_them->post_title.'</a><br/>';
}
}
return $output;
}
echo list_certain_pages('2,10,89');
?>
This also works. I included a call the database that can be cached. And if someone were to already have my wordpress menu installed, you could use the already cached function called ‘get_pa_ge ()’ to load up the page array needed to create this little side menu by replacing this line :
‘$these_pages = get_the_pa_ges ();’ with ‘$these_pages = get_pa_ge ();’.
As I gaze into my crystal ball, I can tell what you are thinking: “If Wordpress natively supports ‘inclusion’ in the function….why use your function?”. And the answer from beyond this realm is quite simple. Page Load Time. The wp_list_pages function parses at least 183 lines of code through the various functions required to produce the list while my function uses 21 lines.
Tags: Development, menu, modification, php code, wordpress



You’ve function works great. Thanks.