以下是我设置的当前循环:
<?php
//start products category loop
$args = array(
\'hide_empty\' => 0,
\'parent\' => 0,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'taxonomy\' => \'profile_categories\',
\'pad_counts\' => 1
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<?php
// setup the category ID
$cat_id = $category->term_id;
// Get category name
$cat_name = $category->name;
// Get category count
$cat_count = $category->count;
//get the category url
$cat_url = get_term_link( $category->slug, \'profile_categories\' );
echo "<h2>";
echo $cat_name;
$subcat_args = array(
\'hide_empty\' => 0,
\'orderby\' => \'name\',
\'child_of\' => $cat_id,
\'order\' => \'ASC\',
\'taxonomy\' => \'profile_categories\',
\'pad_counts\' => 1
);
$subcategories = get_categories($subcat_args);
$subcat_count = count($subcategories);
if ($subcat_count > 0) { echo \' (\'.$subcat_count.\' Subcategories)\'; }
echo "</h2>";
echo \'<a href="\';
echo $cat_url;
echo \'">\';
echo __(\'View all\',\'roots\');
echo "</a>";
// Get posts belonging to current category
$post_args = array(
\'posts_per_page\' => 4,
\'post_type\' => \'moulding_profiles\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'profile_categories\',
\'field\' => \'id\',
\'parent\' => 0,
\'terms\' => $cat_id
)
)
);
$posts_query = new WP_Query($post_args);
if ($posts_query->have_posts() ) {
echo \'<ul>\';
while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<li><a href="<?php the_permalink();?>"><?php the_title(); ?></a> -> Width: <?php the_field(\'mp_profile_dimensions_width\',get_the_ID()); ?> Height: <?php the_field(\'mp_profile_dimensions_height\',get_the_ID()); ?></li>
<?php endwhile;
echo \'</ul>\';
} // done our wordpress loop. Will start again for each category
wp_reset_query(); // reset the query ?>
<?php } // done the foreach statement ?>
这样做的目的是在名为
profile_categories
, 然后列出
4
每个类别中的项目。category参数正在使用
parent => 0
这样只显示顶级类别。我想做的是
pad_counts => 1
不仅要获取类别的计数,还要获取其中所有子类别的计数。如果我想对了,
parents
与冲突
pad_counts
. 我已经看过了
codex 关于这一点,我陷入了困境,如果这是可能的,或者如果我是那么无知,错过了一些明显的东西。任何帮助都将不胜感激。谢谢
Update: 使用http://codex.wordpress.org/Function_Reference/get_categories 作为参考,当parent
设置为0
(要仅显示顶级类别$category->count
和$category->category_count
不包括子类别的计数-仍然认为这是一个bug,除非有人知道解决方法。
最合适的回答,由SO网友:Stephen Harris 整理而成
这可能是一个bug*-但它似乎与您链接的bug不同。问题在于get_terms
作用具体如下:http://core.trac.wordpress.org/browser/trunk/wp-includes/taxonomy.php#L1397
术语数组(当parent=0
, 只是顶级术语)传递给_pad_term_counts
上面写着:
将子级计数添加到父级计数。
通过包含子术语中的项目重新计算术语计数。Assumes all
relevant children are already in the $terms argument
所以_pad_term_counts
职能部门的工作做得很好,但它只得到了最高级的条款,因此只计算这些条款中的职位,而不计算任何子条款。
一个解决方法(这不是特别有效)是获取所有类别,然后过滤掉除顶级类别以外的所有类别。那么就有get_categories
(等等get_terms
) 关注-你是在所有条款之后,所以所有条款都被计算在内:
//The args. Don\'t set parent
$args = array(
\'hide_empty\' => 0,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'taxonomy\' => \'category\',
\'pad_counts\' => 1
);
//I\'ll leave it to you to check for error objects etc.
$categories = get_categories($args));
$categories = wp_list_filter($categories,array(\'parent\'=>0));
也可能不是:这只是排除了与标准不匹配的术语数量