Modify The WordPress Pages Widget to Include Pages Instead of Exclude Pages
Posted by alex in Insights at 7:02 AMRecently I was setting up a WordPress blog and needed to use the default pages widget to list a certain set amount of pages. On this blog I used the NextGEN Gallery for dozens of different galleries. The issue was that I wanted to only list a couple of pages using the default Pages widget and when using NGG I would have dozens if not hundreds of pages that I would need to exclude with the default Pages widget. So what I needed to do was include a couple of page ID’s instead of excluding hundreds of page ID’s.
The problem above can be accomplished by making one minor adjustment to one WordPress file. We will need to modify the wp_pages_widget PHP function to include the pages specified in the admin versus excluding them.
File to Modify: <wp-root-directory>/wp-includes/widgets.php
Line: 682
Replace The Below:
$out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
With:
$out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'include' => $exclude) );
Notice you only modify one exclude with include. Do not change each instance! So now whatever Page ID’s you enter into the “Exclude” configuration box under the Pages Widget will be included instead of excluded. If you want to also modify the below line in the same file to display Include in the Pages Widget instead of Exclude.
File: <wp-root-directory>/wp-includes/widgets.php
Line: 734
Replace:
<label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" cla ss="widefat" /></label>
With:
<label for="pages-exclude"><?php _e( 'Include:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" cla ss="widefat" /></label>
Again the only thing you are changing out is Exclude: with Include:.
Some people may search for the wp_list_pages function to accomplish the same goal but I found it much easier to modify the wp_pages_widget since it will be rare for me to ever use the wp_list_pages PHP function again.
Related posts:
- WordPress: Display Pages Marked Private To All Users When I was initially attempting to do this it was...
- Exclude One Port from a TCPDUMP Output Excluding one port from a tcpdump output is easy. Use...
- Linux Command cat Exclude Lines with Comments Using grep If you want to see the actual active lines from...
- How to Add a New Define Pages Page to the Information SideBox in ZenCart This article describes how to add a link to the...
- WordPress JPF Login/Out Plugin Logout Not Working After Upgrading to WordPress 2.7 I use the JPF Login/Out plugin to display text at...








Entries (RSS)
PHP as a technology has gained enterprise acceptance.
thanks for your post. It was helpful.
[Reply]
alex Reply:
May 6th, 2009 at 12:32 PM
Glad it helped out.
[Reply]
Thank you Man!!
[Reply]
alex Reply:
January 19th, 2010 at 12:38 PM
Hello Samsó,
Sure man… no problem. Glad the article helped you out.
Thanks.
alex
[Reply]
updating wordpress changes will be lost?
[Reply]
alex Reply:
June 18th, 2010 at 7:38 AM
Hello Mic3000,
Not sure I understand the question you are asking…. If you could provide more details we will try to provide an answer.
Thanks.
alex
[Reply]
nice widget, i hope i try for my blog
[Reply]
alex Reply:
September 24th, 2010 at 6:36 PM
Hello free widget,
Thanks. Hope it works out for you.
Thanks.
alex
[Reply]
Is there a way to add this to functions.php or custom_functions.php so that it doesn’t get lost when WP is upgraded?
[Reply]
alex Reply:
November 6th, 2011 at 2:32 PM
Hello Michelle,
It would likely take creating a plugin so it was completely isolated from any of the code base. I just needed a quick and dirty fix and wanted to note it for others to use. I do this a lot when in fact the proper way would be through the use of plugins. In my case I keep a list of any modifications I make to various sites so I can make them again after upgrades.
Thanks.
alex
[Reply]