views:

87

answers:

2

Hello.

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.

+1  A: 

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>'; } ?>
songdogtech
It's working and wrapping `<a>`'s into `<span>`. But i can't use `current_category=1` argument. So it can't add `.current-cat` class to `<li>`s.
fatihturan
You're right; that complicates things. Let me think about it....
songdogtech
+3  A: 

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.

Doug Neiner