Remove wp_list_pages() Links for Parent Pages

Creating a page list in WordPress is super easy. All it takes is one function call: <?php wp_list_pages(); ?>. The only problem with the list is if you have sub-pages categorized under pages that you only want to use as a way to organize your pages. Your parents pages are still linked to in the list but most likely they will be empty. I decided to create a quick function to solve this problem.

Just place this in your theme’s functions.php file.

", $pages);
$count = 0;
foreach($pages as $page) {
if(strstr($page,"
    ")) { $page = explode('
      ', $page); $page[0] = str_replace('','',$page[0]); $page[0] = preg_replace('/\/','',$page[0]); if(count($page) == 3) { $page[1] = str_replace('','',$page[1]); $page[1] = preg_replace('/\/','',$page[1]); } $page = implode('
        ', $page); } $pages[$count] = $page; $count++; } $pages = implode('',$pages); echo $pages; } ?>

Now just replace your wp_list_pages(); function with removeParentLinks(); and away you go.
NOTE: This only works for page lists that are three levels deep or less.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie