Remove wp_list_pages() Links for Parent Pages
by Bandicoot Marketing on | Posted in Tutorials | 44 comments
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.
<?php function removeParentLinks() { $pages = wp_list_pages('echo=0&title_li='); $pages = explode("</li>", $pages); $count = 0; foreach($pages as $page) { if(strstr($page,"<ul>")) { $page = explode('<ul>', $page); $page[0] = str_replace('</a>','',$page[0]); $page[0] = preg_replace('/\<a(.*)\>/','',$page[0]); if(count($page) == 3) { $page[1] = str_replace('</a>','',$page[1]); $page[1] = preg_replace('/\<a(.*)\>/','',$page[1]); } $page = implode('<ul>', $page); } $pages[$count] = $page; $count++; } $pages = implode('</li>',$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.
44 comments for “Remove wp_list_pages() Links for Parent Pages”