How to Remove the Links to Parent Pages in the WordPress Page List

Posted on June 13, 2009  |  Category: Tutorials

Picture 1Creating 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.


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

Short URL: http://bavotasan.com/?p=674

31 Responses to “How to Remove the Links to Parent Pages in the WordPress Page List”

  1. Kevin

    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

    #2988
    • 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.

      #2991
  2. 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?

    #3034
  3. 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.

    #3130
  4. von

    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!

    #3156
  5. Niesom

    Fantastic tutorial and it works without any problem!

    #3392
  6. 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.

    #3426
  7. ETTR

    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!

    #3697
  8. Jimmy Mac

    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;
    }

    #3872
  9. Jimmy Mac

    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;
    }
    ?>

    #3881
  10. ETTR

    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.

    #3923
  11. ETTR

    Again:

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

    #3924
  12. jerome robins

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

    any solution to this problem?

    #3974
    • 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.

      #3987
    • ETTR

      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;
      }

      #3993
    • 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;
      }
      
      #3999
  13. Jimmy Mac

    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…!

    #3979
  14. ETTR

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

    #4130
  15. 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!

    #4622
  16. adeneba

    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)

    #5325
    • Grenouye

      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

      #8209
  17. 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!

    #5379
  18. Lou

    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.

    #6126
  19. elia

    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

    #6811
  20. elia

    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

    #6812
  21. Daniel

    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!

    #7862
    • 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.

      #7865
  22. maj

    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!

    #8269
  23. Now just replace your wp_list_pages(); function with removeParentLinks();

    Where do I change this?

    #8303

Leave a Reply

To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.