• I have a function to return a category menu, I am trying to add a ‘color’ class to the returned menu variable.

    Lets say we have category $i = 1 and $style=’green’

    $cat_class = 'cat-item cat-item-' .$i;
    $amended_class = $cat_class .' ' .$style;
    $catlist = str_replace($cat_class, $amended_class, $catlist );

    This works as expected as:
    cat-item cat-item-1 will become
    cat-item cat-item-1 green

    Thats great but it will replace all instances, so we then hit unlucky ’13’
    cat-item cat-item-13 will become
    cat-item cat-item-1 green3

    I have tried adding a space to the $cat_class:
    $cat_class = ‘cat-item cat-item-‘ .$i .’ ‘;

    But this does not work!

    I am using the same code for category ancestors so it means that these will be doing the same and splitting the 13 if 1 and 13 are in the same tree.

    Any ideas?

    David

Viewing 1 replies (of 1 total)
  • Thread Starter Digital Raindrops

    (@adeptris)

    Ok figured it out!

    if( $style ) {
    	/* Normal Category Menu Item */
    	$cat_class = '"cat-item cat-item-' .$i .'"';
    	$amended_class = '"cat-item cat-item-' .$i .' ' .$style .'"';
    	$catlist = str_replace($cat_class, $amended_class, $catlist );
    	/* Parent Category Menu Item */
    	$cat_class = '"cat-item cat-item-' .$i .' current-cat-parent"';
    	$amended_class = '"cat-item cat-item-' .$i .'  current-cat-parent ' .$style .'"';
    	$catlist = str_replace($cat_class, $amended_class, $catlist );
    	/* Ancestor Category Menu Item */
    	$cat_class = '"cat-item cat-item-' .$i .' current-cat-ancestor"';
    	$amended_class = '"cat-item cat-item-' .$i .'  current-cat-ancestor ' .$style .'"';
    	$catlist = str_replace($cat_class, $amended_class, $catlist );
    }

    This may help someone else!

    David

Viewing 1 replies (of 1 total)

The topic ‘str_replace problem!’ is closed to new replies.