thequerist thequerist - 1 year ago 46
PHP Question

Adding a title attribute to category without affecting core

My current setup includes having the category of each post. But whenever I mouse over the category, it shows the default message of

Show all posts in [category]

I have looked around and have seen some pretty similar questions that require multiple lines of php code. This surely cannot be that complicated. Is there a way I can perhaps tweak the following to get it to do what I want?

<?php the_category(' '); ?>

This is the code that returns the above. I am looking through the codex and found
. I am kind of new to arrays and am having problems figuring them out. I was kind of hoping that something like this would work:

<?php the_category('title=category_description()'); ?>

but it is not. I know that is a pretty hilarious way of solving this to most of you but I am completely lost. I just want the description of the category to be the title attribute. Is there a simple way that I can do this?

Answer Source

wordpress codex

using get_the_category(); you can save all of the categories into a variable. Ex:

$categories = get_the_categories();

There is however an optional parameter that you can pass; the category ID. Ex:

$categories = get_the_categories(THE_CATEGORY_ID);

Whether you present a category ID or not you must run $categories through a loop. Ex:

foreach($categories as $category){


Only then, will you be able to customize your title attribute without modifying the wordpress code. Ex:

foreach($categories as $category){
    echo '<a href="'.get_category_link($category->term_id ).'" title="'.YOUR_CUSTOM_TITLE.'">'.$category->cat_name.'</a>';