Jun
13
2009

Remove wp_list_pages() Links for Parent Pages

by   |  Posted in Tutorials  |  40 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&amp;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.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/9JOFKE

Discussion 40 Comments

  1. Kevin on June 20, 2009 at 1:18 pm

    Can this code we modified to included sub-catagories that exceed 3?

    Is it a limited to 3 levels because you set the value (if(count($page) == 3)) to 3 for the purpose of the example or because it breaks with more than 3 levels?

    Thank you
    Kevin

    • c.bavota on June 20, 2009 at 3:28 pm

      There is probably a way to make it works for more but I haven’t been able to wrap my head around how to do that… just yet.

    • gurinder on January 25, 2011 at 5:52 am

      good

  2. steve on June 23, 2009 at 12:06 pm

    Greetings. I love your magazine theme. I searched (literally l spent most of three days searching)before settling on yours. You had stiff competition – but yours was my personal choice and the favorite of our team. WELL DONE. I have one small problem – if I layout the site with two columns on the left, both appear on thele3ft and are orderly. If I choose two columns on the right, both appear UNDER the rest of hte page – they function like footers. If I shoose one right and one left (my prefered layout) the one on the left is fine but the one that is supposed to be on the right is, again, laid out like a footer. Can you help me straighten this out?

  3. Alberto on June 27, 2009 at 5:50 am

    Very interesting article. I’m planning to write a post on the same subject on my blog soon; I’ll cite yours.
    PS: congratulations for your beautiful wp-themes! I’m using your “magazine” on my blog.

  4. von on June 30, 2009 at 10:35 am

    If I edit the functions.php, I encountered error in wp-admin. The error display everytime I make changes or post articles. Can anyone explain why? Thanks!

  5. Niesom on July 20, 2009 at 5:52 am

    Fantastic tutorial and it works without any problem!

  6. Jason on July 22, 2009 at 4:21 pm

    Does this remove links from all parent pages or can it be used for individual pages. I want some parents to be clickable and some not.

    • c.bavota on July 22, 2009 at 8:15 pm

      If the parent page has a child it removed the link.

  7. Kadix on August 6, 2009 at 2:18 pm

    the Plugin Page List Plus
    http://wordpress.org/extend/plugins/page-lists-plus/

    solves this problem

    • Francis Perron on April 12, 2011 at 11:25 am

      Thank you, I use it on http://www.diplomate.com and it work fine !!
      This site is temporary, while I’m creating the new one. So check the site back in 1 month.

  8. ETTR on August 7, 2009 at 7:31 am

    Hi everyone. I want to do the same thing, but for parent categories (not for parent pages).
    I tried to replace $pages = wp_list_pages(‘echo=0&title_li=’);
    with $pages = wp_list_categories(‘echo=0&title_li=’); in the functions.php code.
    And ofcourse I did also replaced wplistcategories (); with removeParentLinks();
    But unfortunately it has no effect at all. Please help me on this! Thanks!

  9. Jimmy Mac on August 17, 2009 at 6:07 am

    Try the following for categories.

    Worked for me on 2.8.4 but will probably break your CSS.
    You’ll have to restyle the category list based on the new nest…

    function removeParentCatLinks() {
    $cats = wp_list_categories(‘echo=0&title_li=’);

    $cats = explode(“”, $cats);

    $count = 0;
    foreach($cats as $cat) {

    if(strstr($cat,”")) {
    $cat = explode(“”, $cat);

    $cat[0] = str_replace('</a>','',$cat[0]);
    $cat[0] = preg_replace('/\<a(.*)\>/','',$cat[0]);

    $cat = implode("<ul class='children'>", $cat);

    }
    $cats[$count] = $cat;
    $count++;
    }
    $cats = implode(”,$cats);
    echo $cats;
    }

  10. Jimmy Mac on August 17, 2009 at 6:43 pm

    Damn code snippets… please ignore previous post as the tags got garbled…

    <?php
    function removeParentCatLinks() {
    $cats = wp_list_categories(‘echo=0&title_li=&exclude=1,18′);
    $cats = explode(“</li>”, $cats);
    $count = 0;
    foreach($cats as $cat) {

    if(strstr($cat,”<ul class=’children’>”)) {
    $cat = explode(“<ul class=’children’>”, $cat);
    $cat[0] = str_replace(‘</a>’,”,$cat[0]);
    $cat[0] = preg_replace(‘/\<a(.*)\>/’,”,$cat[0]);
    $cat = implode(“<ul class=’children’>”, $cat);
    }
    $cats[$count] = $cat;
    $count++;
    }
    $cats = implode(‘</li>’,$cats);
    echo $cats;
    }
    ?>

  11. ETTR on August 20, 2009 at 1:06 pm

    Thanks Jimmy, that did the trick!

    1) Copy this code in functions.php
    2) Replace your function with
    (In my case located in header.php)

    But as you were saying, the css is messed up now. And I can’t figure out how to restyle it in my css.

  12. ETTR on August 20, 2009 at 1:09 pm

    Again:

    1) Copy this code in functions.php
    2) Replace your function wp_list_categories(”); with removeParentCatLinks(”);
    (In my case located in header.php)

  13. jerome robins on August 23, 2009 at 9:38 am

    this works but it ends up making the font on my nav tiny (on the ones with children).

    any solution to this problem?

    • c.bavota on August 24, 2009 at 11:06 am

      That sounds like a CSS problem. Because the links are now stripped, the CSS for the anchor tag will not apply to your parent links. You need to add the CSS to non-links as well.

    • ETTR on August 24, 2009 at 6:34 pm

      Hi Bavota, can you give an example how we can fix this in the css? Do I need to add something after “a:link”

      nav li a, #nav li a:link, #nav li a:visited {

      color: #FFFFFF;
      display: block;
      font-weight: normal;
      margin: 0px;
      padding: 9px 15px 10px 15px;
      text-decoration: none;
      border-right: 1px solid #000000;
      }

    • c.bavota on August 25, 2009 at 10:56 am

      Hey ETTR,

      In your example, you just need to add a #nav li to the style tags so that is looks like this:

      #nav li a, #nav li a:link, #nav li a:visited, #nav li {
      color: #FFFFFF;
      display: block;
      font-weight: normal;
      margin: 0px;
      padding: 9px 15px 10px 15px;
      text-decoration: none;
      border-right: 1px solid #000000;
      }
      
  14. Jimmy Mac on August 23, 2009 at 9:29 pm

    All I can suggest guys is to do what I did…

    View the source of your page as it is now and work out the new list structure and adjust your css accordingly. Different themes will have different css applied so I think it’s a case of working these things out on an individual basis. Good exercise in really learning and understanding css though…!

  15. ETTR on September 3, 2009 at 4:28 pm

    Ok thanks guys. Problem solved, css is ok now (took me a little while) ;)

  16. oes tsetnoc on September 18, 2009 at 8:18 am

    I’m really enjoying your tutorials, I have save most and gonna print them so that it is easier to read and to store.

    I didn’t thought that i could remove those links, LOL.
    Great site!

  17. adeneba on October 8, 2009 at 8:42 am

    Another solution (to remove parent categories links) with this function :

    &lt;?php
     
    // Supprimer les liens parents dans la liste des cat?©gories
    // Remove parents links in categories
     
    function removeCategoriesParentLinks($arg,$addFakeLink) {
    if ($addFakeLink==true) {
      $tagStart=&quot;<a href=";" rel="nofollow">";
      $tagEnd="</a>";
    } else {
      $tagStart=$tagEnd="";
    }
    $cats = wp_list_categories($arg);
    $cats = explode("", $cats);
    $count = 0;
    foreach($cats as $cat) {
    if(strstr($cat,"")) {
      $cat = explode("", $cat);
      $cat[0] = str_replace('</a>',$tagEnd,$cat[0]);
      $cat[0] = preg_replace('/\<a>]+)\&gt;/',$tagStart,$cat[0]);
      if(count($cat) == 3) {
        $cat[1] = str_replace('</a>',$tagEnd,$cat[1]);
    	$cat[1] = preg_replace('/\<a>]+)\&gt;/',$tagStart,$cat[1]);
      }
      $cat = implode("", $cat);
    }
    $cats[$count] = $cat;
    $count++;
    }
    $cats = implode('',$cats);
    return $cats;
    }
    ?&gt;

    call it with removeCategoriesParentLinks(); instead of wp_list_categories();

    Notes :
    - You can send wp_list_categories() arguments ;
    - the additional argument ‘$addFakeLink’ (=true/false) is used to add fake links my categorie (useful to
    maintain css style)

    • Grenouye on March 11, 2010 at 9:23 am

      Hello adeneba
      I tried your code which seems to be what i need but there is no results. My parent categories are still linked. can you help me to see if i didn’t made a mistake somewhere ?
      I put this :

      &lt;?php
       
      // Supprimer les liens parents dans la liste des cat?©gories
      // Remove parents links in categories
       
      function removeCategoriesParentLinks($arg,$addFakeLink) {
      if ($addFakeLink==true) {
        $tagStart=&quot;<a>";
        $tagEnd="</a>";
      } else {
        $tagStart=$tagEnd="";
      }
      $cats = wp_list_categories($arg);
      $cats = explode("", $cats);
      $count = 0;
      foreach($cats as $cat) {
      if(strstr($cat,"")) {
        $cat = explode("", $cat);
        $cat[0] = str_replace('</a>',$tagEnd,$cat[0]);
        $cat[0] = preg_replace('/\<a>]+)\&gt;/',$tagStart,$cat[0]);
        if(count($cat) == 3) {
          $cat[1] = str_replace('</a>',$tagEnd,$cat[1]);
      	$cat[1] = preg_replace('/\<a>]+)\&gt;/',$tagStart,$cat[1]);
        }
        $cat = implode("", $cat);
      }
      $cats[$count] = $cat;
      $count++;
      }
      $cats = implode('',$cats);
      return $cats;
      }
      ?&gt;

      in the fonctions.php of my theme,
      and i change wp_list_categories
      into removeCategoriesParentLinks
      like this :

       
       
       
       
       
       
       
       
       
      		 <!-- header_categories_menu -->

      Is there something wrong in what i did ?
      Thanks a lot

    • adeneba on March 23, 2010 at 8:47 pm

      Hi Grenouye
      My code above has been corrupted when I pasted it in the comment form.
      You should retrieve the correct function here :
      http://wordpress.org/support/topic/240465

    • adeneba on March 23, 2010 at 11:13 pm

      Hey… if your nickname is grenouye, mine is “laray-net” !!

  18. ahmed hadra on October 12, 2009 at 7:47 am

    And other solution ind if you are using a cufon drop-down menu is to use this plugin:

    http://www.technokinetics.com/plugins/page-lists-plus/

    it disables parents links and better, you can change the link of the parent and point it to the first child (more logical than disable the link) or what ever you want.

    great post!

  19. Lou on November 13, 2009 at 7:36 am

    Hello everyone!

    adeneba,
    I’ve got the Enhanced Categories plugin from

    http://wordpress.org/extend/plugins/enhanced-categories/

    with a customized Apricot theme

    http://www.ardamis.com/2007/06/03/apricot/

    and i’ve got parent categories with sub categories but i can’t seem to make the parent categories not clickable. I’ve searched for days and found you answer in many places but being somewhat of a n00b, i don’t know how to make it work, where to put this and where to call it. Currently the site is in our office internal server so can’t post here for you perusal…

    Any help would be greatly apreciated! Thanks.

    • adeneba on March 23, 2010 at 11:02 pm

      Just append the function in your functions.php file
      (as discribed in the main article of this page)

      and then replace something like :

      wp_list_categories(“echo=0&depth=2&title_li=&exclude=”.$lw_exclude_categories.”");

      by :

      removeCategoriesParentLinks(“echo=0&depth=2&title_li=&exclude=”.$lw_exclude_categories.”",true);

      still in your functions.php file and wherever it’s needed

      As mentioned above, the function has been messed up when I copied it in the form… Check the link below :
      http://wordpress.org/support/topic/240465

  20. elia on December 18, 2009 at 7:32 am

    hi,
    good article but it doesn’t work for me.. if i copy

    in my functions.php my site page became totally white!

    thanks
    elia

  21. elia on December 18, 2009 at 7:33 am

    hi,
    good article but it doesn’t work for me.. if i copy

    &lt;?php
    function removeParentLinks() {
    $pages = wp_list_pages(&#039;echo=0&amp;title_li=&#039;);
    $pages = explode(&quot;", $pages);
    $count = 0;
    foreach($pages as $page) ......?&gt;

    in my functions.php my site page became totally white!

    thanks
    elia

  22. Daniel on February 23, 2010 at 9:31 am

    Hey all,
    Thanks for this snippet.
    I have one problem. I want to style this ‘non-linked’ text, and to do that i need to put the text in tags.
    How can i do this? I’m not familiar with php.

    (and I don’t want to style my text by #nav li, because it will mess up other things).
    Great thanks!

    • c.bavota on February 23, 2010 at 12:34 pm

      Right now you are replacing the anchor tags with nothing. You can add a tag between the two single quotes of the preg_replace and str_replace functions and then the anchor tags will be replaced with whichever tag you include.

  23. maj on March 14, 2010 at 11:46 pm

    nice one. thank you. more flexible than page list plus plugin. plugins may have conflicts with other plugins resulting to nonfunctional plugins. :) ) thank you. more power!

  24. ahoodie on March 16, 2010 at 11:32 am

    Now just replace your wp_list_pages(); function with removeParentLinks();

    Where do I change this?

  25. Michael on May 12, 2010 at 6:52 pm

    Hi,

    I’ve recently released a plugin to unlink parent pages. It’s a little more automatic than the solution described here, since it does not require editing any template files. It can unlink pages to any depth in a page hierarchy, and it also solves the problems some people are having with CSS styling, by offering the option to use “dummy links” (that is, links that lead to “#”). If anyone tries it, I’d be interested to hear how it worked for you.

    http://www.ambrosite.com/plugins/unlink-parent-pages-for-wordpress