Shaun
Shaun

Reputation: 811

Removing a phrase from a php string

I'm building a website in Wordpress and wish to have a list of catogories on a seperate page which acts as a filter for the isotope plugin. So far i've managed to get this working however i now need to remove the phrase "academia" from each category as this is can only be seen in the backend of wordpress but when trying to use str_replace it doesn't render any results.

Is anyone kind enough to some help? :)

link to website : http://www.bluemoontesting.co.uk/intbauwp/testing/#

<?php

    $terms = get_categories('orderby=name&depth=1&title_li=&use_desc_for_title=1&parent=34');  // get all categories, but you can use any taxonomy
    $remove = str_replace("academia","", $terms);
    $count = count($remove); //How many are they?


    if ( $count > 0 ) {  //If there are more than 0 terms

        foreach ( $terms as $term ) 

        {  //for each term:
            echo "<li><a href='#' data-option-value='.".$term->slug."'>" . $term->name . "</a></li>\n";
            //create a list item with the current term slug for sorting, and name for label
        }
    }
?>

Upvotes: 0

Views: 54

Answers (2)

Nicolas
Nicolas

Reputation: 1143

You should echo out $terms and make sure it contains "academia". Your code should work as is without a for loop as the string isn't an array. My guess is that $terms may not contain the string you're looking for.

Upvotes: 0

user2226755
user2226755

Reputation: 13159

Add str_replace in foreach.

$name = str_replace("academia","", $term->name);

Like this :

if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        $name = str_replace("academia","", $term->name);
        echo "<li><a href='#' data-option-value='.".$term->slug."'>" . $name . "</a></li>\n";
    }
}

Upvotes: 1

Related Questions