2016-10-08 197 views
2

这是我有:

$args = array(
     'type' => 'post', 
     'child_of' => 0, 
     'parent' => '', 
     'orderby' => 'name', 
     'order' => 'ASC', 
     'hide_empty' => 1, 
     'hierarchical' => 1, 
     'exclude' => '', 
     'include' => '', 
     'number' => '', 
     'taxonomy' => 'directory-category', 
     'pad_counts' => false 
    ); 

这让我的类别。显示子类别 - WordPress的

我想要的是获取此directory-category分类的子类别。

关于如何做到这一点的任何想法?

我不是要求解决方案,只是建议或某人向我展示道路。

谷歌搜索并没有帮助:/

下面是截图HERE

回答

1

你说你不想直接回答,但本质上你想使用这里找到get_terms:

https://developer.wordpress.org/reference/functions/get_terms/

SELECT * from prod_term_taxonomy WHERE parent = 0;

UPDATE:

// Using your specific parent taxonomy id of 214 the query is below 
global $wpdb; 
$results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'"); 

// then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this... 

$child_cat_array = array(); 

foreach ($results as $result) { 
    $term = get_term($result->term_id, $taxonomy); 
    $name = $term->name; 
    // the slug will be used for querying for posts 
    $slug = $term->slug; 
    // this will push the slug of the child category into the array for querying posts later 
    array_push($child_cat_array, $slug); 
} 

然后,您可以修改您的get_posts这样的查询:

$args = array(
    'tax_query' => array(
     array(
      'taxonomy' => 'directory-category', 
      'field' => 'slug', 
      'terms' => $child_cat_array 
     ) 
    ) 
); 

$postslist = get_posts($args); 
+0

你希望得到与所有这些子类别相关的帖子吗? –

+0

感谢您的建议。我已经检查过'get_terms()',但我正在寻找一些东西,只是更具体一些,lol –

+0

您可能需要用全局$ wpdb查询WordPress数据库。如果你愿意,我可以给你答案:)你熟悉SQL吗?因为你会查询他们住在你的WordPress数据库中的确切表格。不幸的是,没有内置的WordPress功能来检索它们。 –