WordPress.org

Forums

[resolved] Allowing HTML Tags in a Category List (4 posts)

  1. SpankMarvin
    Member
    Posted 5 years ago #

    Hi everyone

    I searched for this but was surprised I couldn't find a similar enquiry...

    I have a sidebar in which I list all the sub-categories of a particular category. No problems there. BUT, I want to be able to add simple html tags to the category name (e.g. add an em tag to a name of a project).

    Going to Edit the category from dashboard and adding html tags doesn't work as there appear to be some kind of striptags function applied once I save changes. Adding the entity equivalent, predictably, just shows the tags AS TAGS rather than as the parsed tag.

    Does anyone know of either a plugin or a code hack which can disable the striptags function from category titles? I'd prefer a plugin route if possible but am willing to get my hands dirty with the code if necessary.

    Thanks in advance for any help

    John

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    I assume you're currently using wp_list_categories. If so, you might be able to apply display changes through CSS but, without seeing an example of one of your pages, it's hard to know for sure.

    Otherwise, you could replace wp_list_categories with get_categories which would then give you an array which you could loop through, add markup as needed and then display.

  3. SpankMarvin
    Member
    Posted 5 years ago #

    Thank you -- I'm using wp_list_categories. Thing is I only want a couple of words formatted. In an ideal world, adding the tags at the category editing stage would be the way to do it. Hard coding the actual formatting is probably a bit overkill, and the CSS option is an all-or-nothing solution.

    It's not hugely important but I am interested in whether there's a workaround which is nice and convenient!

    Mind you, I'm thinking about this. Adding a special, homemade markup, such as ::format:: and then cycling through the array as you suggest, then using a replace function, would allow me to do that from within category editing. Hmm. Let me try that and I'll report back.

    Thank you for the inspiration!

    J

  4. SpankMarvin
    Member
    Posted 5 years ago #

    Hmm, ok, there were issues with depth that made it very difficult, and beyond my capabilities, to use get_categories (there are sub-categories and I needed to use current-cat classes).

    I have resolved this, however.

    In the category-template.php file, I simply found the following lines of code in the wp_list_categories function:

    if ( $echo )
    		echo $output;
    	else
    		return $output;

    and modified it to

    if ( $echo ) {
    		$output = html_entity_decode($output);
    		echo $output;
    	} else {
    		return $output;
    	}

    This way, I can add the html entities to the category titles, and they are then parsed correctly by wp_list_categories AS TAGS rather than as text.

    esmi, thanks for your input, it put me on the right track.

    J

    EDIT: There's a far easier and more contextual way of achieving the above without going through changing the template. Keep the category-template.php as it was!

    I forgot that there is an "echo" parameter in wp_list_categories. So, using the following lines in my sidebar (where the categories are being listed):

    $cat_list = wp_list_categories('child_of=3&depth=2&hide_empty=0&title_li=&echo=0');
    	$cat_list = html_entity_decode($cat_list);
    	echo($cat_list);

    I was able to achieve precisely what I was after and keep the depth functionality.

Topic Closed

This topic has been closed to new replies.

About this Topic