WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages

Date: 29th January 2009 at 11:47 pm | Filed under: development, scripts, wordpress | Author: Sam Burdge | Tags: , , ,

I noticed in the WordPress forums that a lot of people were asking how to remove the title="example" attribute from the links generated by the WordPress template tags wp_list_categories and wp_list_pages. The title attribute is what generates the little text box with something like "View all posts filed under <category>" when you hover over a link.

I had previously devised a 'hack' for removing the dreaded title attribute for a theme I worked on. The key is to set the 'echo' parameter of the function to '0' and then to use preg_replace code to remove the title attribute. Here is the code:

wp_list_categories:

<?php
$cool_cats = wp_list_categories('echo=0');
$cool_cats = preg_replace('/title=\"(.*?)\"/','',$cool_cats);
echo $cool_cats;
?>

wp_list_pages:

<?php
$clean_page_list = wp_list_pages('echo=0');
$clean_page_list = preg_replace('/title=\"(.*?)\"/','',$clean_page_list);
echo $clean_page_list;
?>

Personally I don't mind having the titles on my site navigation, and I use the NAVT plugin to generate my nav, so the template tags don't apply for me.

27 Responses to “WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages”

  • Comment by pressitfor.me
    Date: January 29th, 2009 at 11:54 pm

    WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages | Sam Burdge…

    I noticed in the WordPress forums that a lot of people were asking how to remove the title=”example” attribute from the links generated by the WordPress template tags wp_list_categories and wp_list_pages. The title attribute is what generates the lit…

  • Comment by tom
    Date: February 10th, 2009 at 3:49 pm

    Where would I place the code snippet for wp_list_pages?

  • Comment by Sam Burdge
    Date: February 10th, 2009 at 4:56 pm

    Hi Tom

    The code snippet above is meant to replace the existing wp_list_pages function used in your theme.

    wp_list_pages is usually called in either header.php, footer.php or sidebar.php of your theme.

    More info about wp_list_pages here:
    http://codex.wordpress.org/Template_Tags/wp_list_pages

    Sam

  • Comment by Sumit Khanna
    Date: April 1st, 2009 at 3:39 pm

    Thanks for the code snippit! I had started using nicetitles for http://chattanoogaswing.org and then noticed that wp_list_pages added tiles to all the links!

    The calendar I was using doesn’t add a class for the dates I wanted the tooltips on. If it did, I could just modify the nicetitles javascript to only select that class of element.

    Using your little regex to remove the titles form wp_list_pages is much easier!

    Also, cool theme you’ve got going on here. I like the green-on-black console look.

  • Comment by Designs Genius
    Date: May 19th, 2009 at 10:15 pm

    thanx a lot for this tip !.
    I have another question for everybody : How can I remove a hyperlink from wp_list_pages, but only from the first ul in the heirachy ?
    thx for all who will consider my question ! :)

    Sébastien from France

  • Comment by Frank Karlstrøm
    Date: June 17th, 2009 at 8:30 pm

    Yet another thank you from me. Have been looking for a plugin to do this, but this is even better. Do you think it’s better for me to write a private plugin implementing this small little code? to avoid the snippet to be removed when I upgrade WP.

  • Comment by Robert Carro
    Date: July 6th, 2009 at 5:41 pm

    W9xinK great tips. I enjoyed reading this,

  • Comment by coda
    Date: July 29th, 2009 at 11:49 pm

    Exactly what I was looking for, thanks a lot!

  • Comment by norton
    Date: September 10th, 2009 at 2:59 pm

    Thanks a lot . I needed to remove the “title-attribute” because of a firefox(macOs)-bug that would always reload the dropdown after stoping to move the mouse on it.
    This would not happen if there was no “title-attribute” anymore. To figure out this already took forever. This code finally make my dropdowns work like they should.

  • Comment by JR
    Date: September 11th, 2009 at 2:49 pm

    What happens if it isn’t hard coded into the theme and WP seems to be generating them of its own accord? What file do I need to edit?

  • Comment by Matt Furtado
    Date: September 16th, 2009 at 8:16 pm

    For the wp_list_categories hack, how would I get the category name to display instead of “View all posts filed under “? Is there a simple hack that I’m not aware of?

  • Comment by Matt Brewster
    Date: September 18th, 2009 at 1:53 pm

    Great tip - many thanks :)

  • Comment by Will Stern
    Date: October 8th, 2009 at 4:19 pm

    You rock. Great post.
    To answer JR, and anybody else wondering, open your header.php and look for replace it with his code.
    If there are specifics in your brackets, make sure to include them in his code in addition to echo=0
    Mine looks like this: $clean_page_list = wp_list_pages(’title_li=&depth=2&echo=0′);

  • Comment by Will Stern
    Date: October 8th, 2009 at 4:20 pm

    meant to say look for the php calling wp_list_pages() …the php code got cut out. :(

  • Comment by fatihturan
    Date: November 29th, 2009 at 1:08 pm

    Good tip. Thanks.

  • Comment by Tim Holt
    Date: January 4th, 2010 at 4:03 pm

    Those who aren’t comfortable editing their theme files can use the Remove Title Attributes plugin to solve this problem.

  • Comment by Tom
    Date: January 31st, 2010 at 8:07 pm

    Tasty… RegExp is not my bag!

  • Comment by kevin
    Date: February 23rd, 2010 at 9:21 pm

    Cool exactly what i needed. Thanks!

  • Comment by Khaled Hakim
    Date: June 25th, 2010 at 5:50 pm

    Thank you for this awesome snippet of code. One of my picky clients did not want the titles to appear on hover. I was using the wp_listpages function, of course and I am glad I came across this site.

    What this code does, for those who are not sure, is remove the title that appears when the mouse is over (hover) an item in the list that is generated through the use of the wp_listpages function.

    Cheers.

  • Comment by Vidhu
    Date: July 12th, 2010 at 4:43 am

    Wow… This is what i was looking for… Now its working finely in my website… Thanks for the useful tip…

  • Comment by Nathan
    Date: July 28th, 2010 at 12:42 pm

    I used this code and it removed the titles, but it added the text “Pages” before all of the actual pages and it is messing up my menu, how can I get rid of that text?

  • Comment by Sam
    Date: November 13th, 2010 at 11:42 pm

    Guys can anyone put little bit more information here please!!!

    where in the world we should put that piece of code?

  • Comment by althahabi
    Date: December 15th, 2010 at 9:59 pm

    sorry , but where to find the two functions wp_list_categories
    wp_list_pages

    in the theme twentyten !

    thanx

  • Comment by alex chousmith
    Date: January 27th, 2011 at 3:52 pm

    thanks for the tip, it totally works

  • Comment by Ian
    Date: March 14th, 2011 at 6:25 pm

    Found this via Google leading me to the WP forums, works great, better than editing core, thanks!

  • Comment by Ian
    Date: March 14th, 2011 at 6:27 pm

    @ Nathan, try this:

    wp_list_pages(’echo=0&title_li=’);

    note the title_li=

    that makes the list title equal to nothing! Cheers

  • Comment by marion
    Date: March 22nd, 2011 at 3:21 pm

    Thank you, this is perfect! Your solution worked for my categories and custom taxonomies. I wish you can also write a code to remove the parent category links =D

Leave a Comment