【发布时间】:2022-01-17 21:18:44
【问题描述】:
当然,这可以算作重复,但我无论如何都无法完成这个任务,我只是在学习)
有多种分类法,例如location 和set。我需要输出两个分类中包含的帖子数量。我的函数现在看起来像这样:
function get_all_property_count($id, $setid)
{
$count = new WP_Query(array(
'nopaging' => true,
'post_type' => 'property',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'location',
'field' => 'id',
'fields' => 'ids',
'terms' => $id,
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'set',
'field' => 'id',
'fields' => 'ids',
'terms' => $setid,
'include_children' => true,
)
)
),
));
return $count->post_count;
}
然后我用它来获取当前分类中发布的帖子的计数器和另一个按 id 发布的帖子(我不确定我是否解释正确()。
<div class="count_property">
<div class="sell">
<span class="strong">Sell:</span> <?php echo get_all_property_count($term->term_id, 30); ?>
</div>
<div class="rent">
<span class="strong">Rent:</span> <?php echo get_all_property_count($term->term_id, 31); ?>
</div>
</div>
其中$term->term_id,类别ID 来自location 分类。 30 和 31 销售和租赁类别来自 set 分类。
在出口处,我在sell 又得到了一个。比如Sell: 2(虽然一般应该是0),Rent: 1(没错)。
【问题讨论】:
-
你是如何得到 $id, $setid 的? $setid 应该始终保持 30 和 31 对吗?因此,如果您目前在位置 A,您想查看该位置有多少房产可供出租和出售?
-
对。 $id 我们从当前位置获取 - 给出正确的值。 $setid = 30 或 31。会有其他的 $setids,但我检查了它们,出了点问题)
标签: php wordpress function taxonomy