我在Wordpress中建立一个网站,希望在一个单独的页面上列出一个catogories列表,作为同位素插件的过滤器。到目前为止,我已经设法使这个工作,但我现在需要删除短语"学术界"从每个类别,因为这只能在wordpress的后端看到,但在尝试使用 str_replace 时,它不会呈现任何结果。
有人能帮忙吗? :)
链接到网站: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
}
}
?>
答案 0 :(得分:1)
在foreach中添加str_replace。
$name = str_replace("academia","", $term->name);
像这样:
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";
}
}
答案 1 :(得分:0)
您应该回显$terms
并确保其中包含"academia"
。您的代码应该没有for循环,因为字符串不是数组。我的猜测是$terms
可能不包含您正在寻找的字符串。