Question

I'm showing categories with this codes. Normally there is no problem.

<?php
$categories = wp_list_categories('current_category=1&hide_empty=0&title_li=&echo=0&link_before=<span>&link_after=</span>');
$categories = preg_replace('/title=\"(.*?)\"/','',$categories);
echo $categories;
?>

But i want to wrap current category's text into <span> tag for showing current category image.

In short: <a href="#">Home</a> to <a href="#"><span>Home</span></a>.

How i can do it with PHP?

Thanks.

Was it helpful?

Solution

Replace your second line of code with this:

$categories = preg_replace(
    array('/title=\"(.*?)\"/','/(<a.*?>)(.*?)(<\/a>)/'),
    array('','$1<span>$2</span>$3'),
    $categories);

And it will continue to remove the title tags as well as add the <span></span> inside each of the <a> tags.

OTHER TIPS

Does this work?:

<?php $categories = get_categories(); foreach ($categories as $cat) {echo '<a href="'.get_option('home').'/'.get_option('category_base').'/'.$cat->category_nicename.'/"><span>'.$cat->cat_name.'</span></a>'; } ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top