从WordPress查询的术语名称中获取术语ID

时间:2013-02-21 作者:ellenmva

我正在尝试从术语名称中获取术语ID,但无法找出此代码不起作用的原因。

$excludes = \'\';
    if($filtering_excludes) {

     $exclude_terms = explode(", ", $filtering_excludes);
        foreach ($exclude_terms as $exclude_term) {

            $term = get_term_by( \'name\',  $exclude_term,  $filtering_tax  );
             $exclude_term = $term->term_id;         

        }

        $excludes= implode(", ", $exclude_terms);

    }
当我在$exclude\\u上执行var\\u转储时,term is具有term id,但$excludes仍返回term名称。

我有类似的代码从name中获取类别id,它可以毫无问题地工作——这就是代码。

$excludes = \'\';
    if($filtering_excludes) {

     $exclude_cats = explode(", ", $filtering_excludes);
        foreach ($exclude_cats as &$exclude_cat) {
             $cat_id = get_category_by_slug($exclude_cat); 
            $exclude_cat = $cat_id->term_id;

        }

        $excludes= implode(", ", $exclude_cats);
    }
有人知道为什么我的$exlcudes for terms返回的是名称而不是id吗?

1 个回复
最合适的回答,由SO网友:chifliiiii 整理而成

检查变量名称:

  $excludes = \'\';
        if($filtering_excludes) {

         $exclude_terms = explode(", ", $filtering_excludes);
            foreach ($exclude_terms as $exclude_term) {

                $term = get_term_by( \'name\',  $exclude_term,  $filtering_tax  );
                 $exclude_term_array[] = $term->term_id;         

            }

            $excludes= implode(", ", $exclude_term_array);

        }

结束

相关推荐

encoded search terms with %20

<form class=\"search-form\" action=\"<?php echo home_url( \'/suche/\' ); ?>\" method=\"get\"> <input type=\"text\" name=\"s\" class=\"s\" value=\"<?php the_search_query(); ?>\" /> 这是我的searchform.php 样板你知道为什么在搜索例如“