Home » Php » php – Removing title from wp_list_categories()

php – Removing title from wp_list_categories()

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to remove/replace the title attribute on the category elements used by WordPress. I’m using WordPress 2.9.1, with the following code:

<div id="categories">
    <h3>Manufacturers</h3>
        <ul>
            <?php str_replace("title=\"View all posts filed under ","",wp_list_categories('use_desc_for_title=0&exclude=1,2,3,4,5&title_li=&hierarchical=1')); ?>
        </ul>
</div>

From what I had read this method used to work on older versions (I had never tried it though). I really don’t want to hack at the interals of WordPress, or have to use a JavaScript hack, for something so straight forward. Any help is appreciated…

Update
below is what is created from the code above…

<div id="categories">
<h3>Manufacturers</h3>
<ul>
<li class="cat-item cat-item-7"><a href="http://localhost/crosstrainers/?cat=7" title="View all posts filed under Featured">Featured</a>
</li>
</ul>
</div>
How to&Answers:

If you don’t want to use the plugin, stripping the code from the Remove Title Attributes plugin you can see the main function used in order to remove the titles from the categories..

open your template/function.php file and insert the following…

function wp_list_categories_remove_title_attributes($output) {
    $output = preg_replace('` title="(.+)"`', '', $output);
    return $output;
}
add_filter('wp_list_categories', 'wp_list_categories_remove_title_attributes');

this will add a new filter replacing the out of the wp_list_categories function used by wordpress and replace it with the function above..

in your code example

<div id="categories">
  <h3>Manufacturers</h3>
    <ul>
      <?php wp_list_categories(); ?>
    </ul>
</div>

Would output as

 <div id="categories">
      <h3>Manufacturers</h3>
        <ul>
          <li class="cat-item cat-item-7"><a href="http://localhost/crosstrainers/?cat=7">Featured</a></li>
        </ul>
    </div>

Were the title=””, has been stripped out completely..
🙂

credits to: Tim Holt & his plugin

Answer:

This is a bit of a late answer on an old post but there’s a much more simple way which doesn’t require plugins or adding to functions.php:

<?php wp_list_categories('title_li='); ?>

or, if using with other customisations to the output:

<?php $args = array (
    'title_li'           => __( '' ),
    'hide_empty'         => 0,
    'show_count'         => 1,
    'use_desc_for_title' => 0,
    'child_of'           => 1
);
wp_list_categories( $args ); ?>

Answer:

There’s a plugin for that maybe this can help at least looking at the plugin code.

http://wordpress.org/extend/plugins/remove-title-attributes/

Answer:

This is probably your best option for anyone looking to do this without any functions.php work

Simply add this to your template.

                <ul class="nav">
                    <?php wp_list_categories( array(
                        'orderby' => 'name',
                        'taxonomy' => 'product_cat',
                        'depth' => 1,
                        'title_li' => '',
                        'hide_title_if_empty' => true,
                        'use_desc_for_title'  => 0,
                        'include' => array( 28, 27, 8, 29, 43, 31 )
                    ) ); ?>
                </ul>

Answer:

By default, wp_list_categories will include the Category description in the title attribute if there is one, or “View all posts filed under category” if there is no description.

I don’t like having the whole description in there. This is what I am using in functions.php to customize the title attribute:

function custom_categories_title($output) {
    $search = '/title=".+"(.*>)(.+)</i';
    $replace = "title=\"View all articles filed under $2\"$1$2<";
    return preg_replace($search, $replace, $output);
}
add_filter('wp_list_categories', 'custom_categories_title');

If you simply wanted to remove the title attribute altogether, you could use

    $search = '/ title=".+"/i';
    $replace = '';