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.
Search

Popular Posts

Small Biz Website Tips Newsletter

Stay up to date with the latest marketing, sales, and service tips and news.

Small Biz Website Tips Newsletter

Stay up to date with the latest marketing, sales, and service tips and news.