views:

38

answers:

1
add_filter('wp_list_categories', 'myCatNoBrk', 10, 1);

function myCatNoBrk($OrgCat) {
       $CatNoBrk = preg_replace('/<br \/>/',',',$OrgCat);
       return $CatNoBrk;
    }

Hello, this is part of a wordpress function that would replace html breaks with commas. How could I alter this to also remove and add 'x' around each element?

in other words: The output code is something like

<a href="xxx">cat1</a><br>
<a href="xxx2">cat2</a><br>
and so on

I'd like to change this to show only ('cat1','cat2') for an array definition

example output:

<?php $grades_array = array('a+','a','a-','b+','b','b-','c+');?>
+1  A: 

I am not 100% sure what you are trying to achieve, but here's a replacement function that lets you specify what you are putting in place of the break tags (<br />)

add_filter('wp_list_categories', 'myCatNoBrk', 10, 1);

function myCatNoBrk($OrgCat,$replacement = ',') {
       $CatNoBrk = preg_replace('/<br \/>/',$replacement,$OrgCat);
       return $CatNoBrk;
    }

UPDATE: try this

add_filter('wp_list_categories', 'myCatNoBrk', 10, 1);

  function myCatNoBrk($OrgCat,) {

       preg_match_all(' /(?<=^|>)[^><]+?(?=<|$)/',$OrgCat,$CatNoBrk,PREG_PATTERN_ORDER);
       return '(\''.implode('\',\'',$CatNoBrk).'\')';
    }
mwotton
zeemy23
OK, so it looks like you want to remove the whole <a href... > as well. Is that right?
mwotton
yes, if possible.
zeemy23
for something like this <?php $grades_array = array('a+','a','a-','b+','b','b-','c+');?>
zeemy23
Answer updated.
mwotton
thanks so much for your time! but no cigar. still gets the same parsing error as in the first comment.
zeemy23
ps, this is the site the theming is working from http://www.walkernews.net/2010/01/09/display-wordpress-categories-in-one-comma-separated-line/
zeemy23
So, the original function is given a string of several links to categories separated by break tags, and returns those same links separated by commas, right?
mwotton
yes, thats what it does. it doesn't cause a parse error.
zeemy23
OK, based on what you've said here, I am not really sure this is the best place to try and pull an array of categories from. It's highly likely the array you're looking for is generated previously and then used to generate the link tags. If you can EITHER use that array, OR use the function that generates that array, you'll be better off.
mwotton
thanks, I'd imagine that would be in this huge file: category_template.php http://codeviewer.org/view/code:132d
zeemy23
the function wp_list_categories() on line 453 is the one that generates the list of links. On line 489 is a call to the function you REALLY want - get_categories(). I am not sure where this is defined.
mwotton
ahh, yes, in here http://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/category.phpi can't figure out what code it generates though. Is it in the array() form?
zeemy23
Yes it does return an array, however the array will contain more than just the names of the categories - it looks like it contains additional information. (PS: Sorry about the delay, I had to go offline)
mwotton