How to Remove the Links to Parent Pages in the WordPress Page List
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.






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
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.
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?
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.
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!
Fantastic tutorial and it works without any problem!
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.
If the parent page has a child it removed the link.
the Plugin Page List Plus
http://wordpress.org/extend/plugins/page-lists-plus/
solves this problem
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!
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;
}
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;
}
?>
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.
Again:
1) Copy this code in functions.php
2) Replace your function wp_list_categories(”); with removeParentCatLinks(”);
(In my case located in header.php)
this works but it ends up making the font on my nav tiny (on the ones with children).
any solution to this problem?
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.
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;
}
Hey ETTR,
In your example, you just need to add a
#nav lito the style tags so that is looks like this: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…!
Ok thanks guys. Problem solved, css is ok now (took me a little while)
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!
Another solution (to remove parent categories links) with this function :
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)
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 :
in the fonctions.php of my theme,
and i change wp_list_categories
into removeCategoriesParentLinks
like this :
Is there something wrong in what i did ?
Thanks a lot
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!
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.
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
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
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!
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.
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!
Now just replace your wp_list_pages(); function with removeParentLinks();
Where do I change this?