get_terms函数返回所有未返回所有结果的子类别

时间:2016-11-21 09:35:28

标签: php wordpress

我正试图获得给定类别的子类别,如下所示:

$this->getChildCategories = function($parent_id) {                          

        $args = array(
            'parent'         => $parent_id,                 
        ); 

        $terms = get_terms('category', $args);

        return $terms;

};

在仪表板中查看时,总共有10个子类别。但是该函数只返回5个结果。

3 个答案:

答案 0 :(得分:1)

您似乎以错误的方式调用该函数。如果您在documentation中引用该功能,请参阅以下内容:

get_term_children( $term, $taxonomy );

所以要获得一个类别的子类别,这就是我们要做的事情:

get_term_children( $term, $taxonomy  ); //where $term is "category_id" and $taxonomy is "category"

由于您的slug不是父类别的ID,您可以通过此function获取ID:

$parent_category = get_category_by_slug('category-slug'); 
$parent_category_id = $parent_category->term_id;

使用上述函数编写我们自己的函数来获取子类:

function get_children_categories($category_slug, $taxonomy_name){
   $parent_category = get_category_by_slug($category_slug); 
   $parent_category_id = $parent_category->term_id;
   //$Uncategorized_id = get_cat_ID('Uncategorized') ;
   $children_categories = get_term_children( $parent_category_id, $taxonomy_name);
   //unset($children_categories[$Uncategorized_id]);
  return $children_categories;
}

上述函数也返回"未分类",因此要删除您可以取消注释上述函数中的注释部分。

您可以通过传递参数来调用函数get_children_categories:

get_children_categories('event', 'category') //shall return all the children category ids.

现在您已通过该类别的所有子类别ID。要获取术语对象,可以使用以下函数:

get_term_by( $field, $value, $taxonomy, $output, $filter )

感谢。

P.s:因为你删除了上一个问题,我必须亲自搜索你。然而,这是您上一个问题的答案。

答案 1 :(得分:0)

您需要禁用默认限制:

'numberposts' => -1,

答案 2 :(得分:0)

您可以传递父类别ID而不是public static class ContractsAbbreviators { [ContractAbbreviator] public static void EnsureTaskIsStarted() { Contract.Ensures(Contract.Result<Task>() != null); Contract.Ensures(Contract.Result<Task>().Status != TaskStatus.Created); } } [ContractClass(typeof(ContractClassForIFoo))] public interface IFoo { Task<int> MethodAsync(int val); } [ContractClassFor(typeof(IFoo))] internal abstract class ContractClassForIFoo : IFoo { public Task<int> MethodAsync(int val) { Contract.Requires(val >= 0); ContractsAbbreviators.EnsureTaskIsStarted(); Contract.Ensures(Contract.Result<int>() == val); Contract.Ensures(Contract.Result<int>() >= 5); Contract.Ensures(Contract.Result<int>() < 10); throw new NotImplementedException(); } } public class FooContractFailTask : IFoo { public Task<int> MethodAsync(int val) { return new Task<int>(() => val); // esnure raises exception // Contract.Ensures(Contract.Result<Task>().Status != TaskStatus.Created); } } public class FooContractFailTaskResult : IFoo { public async Task<int> MethodAsync(int val) { await Task.Delay(val).ConfigureAwait(false); return val + 1; // esnure raises exception // Contract.Ensures(Contract.Result<int>() == val); } } public class Foo : IFoo { public async Task<int> MethodAsync(int val) { const int maxDeapth = 9; await Task.Delay(val).ConfigureAwait(false); if (val < maxDeapth) { await MethodAsync(val + 1).ConfigureAwait(false); } return val; } }

$cat->cat_ID.