Recently 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.
PHP as a technology has gained enterprise acceptance.
thanks for your post. It was helpful.
Glad it helped out.
Thank you Man!!
Hello Samsó,
Sure man… no problem. Glad the article helped you out.
Thanks.
alex
updating wordpress changes will be lost?
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
nice widget, i hope i try for my blog
Hello free widget,
Thanks. Hope it works out for you.
Thanks.
alex
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?
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