How to disable the link to parent pages when using wp_list_pages in WordPress

Last day I was working on one of my clients’ web site, and he made a request that he wanted to avoid links to certain parent pages of his WordPress site. As I was using wp_list_pages for generating dynamic menus, it was not possible to unlink parent pages or each page individually . So I tried to find out a simple way to achieve this function and finally got a small function that will disable links of individual pages.Below is the code that I used, just copy this and paste it in your header.php file is enough.

<script type=”text/javascript”>jQuery(function($) { $(“li.page-item-10”).children(“a”).attr(‘href’, “javascript:void(0)”);});</script>

In the above example code I had given page id as 10, just replace 10 with your page id.Now if you want to disable more than one page, then simply use the below code,

<script type=”text/javascript”>
jQuery(function($) {
$(“li.page-item-10,li.page-item-14,li.page-item-15”).children(“a”).attr(‘href’, “javascript:void(0)”);
});
</script>

Feel free to leave comment here if you are facing any problem while using this function.

About Shino

Shino, the author of this blog is a 28 year old master graduate in Computer Science, a professional programmer and web developer. TechMagics.com is his humble venture to share whatever he knows and learns about programming,technology and web world.

5 thoughts on “How to disable the link to parent pages when using wp_list_pages in WordPress

  1. cam

    WP gives unordered lists that have children the class 'children', you could use that as a reference..

    $(".children").parent().children("a").attr('href', "javascript:void(0)");

Leave a Reply