【问题标题】:How can I get current taxonomy term id?如何获得当前的分类术语 ID?
【发布时间】:2017-07-23 08:59:19
【问题描述】:

我需要获取当前的分类术语 ID。我正在使用此代码。

$args = array(
        'post_type' => 'property',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
                array(
                  'taxonomy' => 'property_category',
                  'field' => 'id',
                  'terms' => array( 84, 99, 98, 97, 92, 91, 95, 100, 87, 96, 94, 93 ),
                  'include_children' => false
            )
        )
    );

正如您在“术语”部分中看到的,我创建了一个包含所有术语的数组,但我需要像当前页面术语 id 为 98 一样,我想仅在数组中设置 ID 98。

让我给你看一个例子: 此网址https://ayanaproperties.com/property-for-sale/london/ 的术语 ID 为 98,该术语仅显示 5 个正确的属性,但在地图部分地图上显示了所有 220 个属性。这是因为在上面的代码中,我在 terms 数组中指定了所有的 ID。

数组中的所有 ID 都使用相同的代码,这就是我需要 if else 条件的原因。

请帮帮我,我怎样才能使用 if else 条件?

谢谢,

【问题讨论】:

  • 我认为您需要根据 term id 准备 terms 数组。不是吗?
  • 显示你准备 $args 的代码
  • @JYoThI 我已经把php代码放在笔的js部分了,看这里:codepen.io/anon/pen/jLNjww
  • whats that id $_POST['propcateg_id'] 为什么你不在你的 args 查询中使用它

标签: php arrays wordpress taxonomy taxonomy-terms


【解决方案1】:

请使用下面将返回当前术语 ID 的行,然后您可以将该值传递到您的数组中。

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;                

【讨论】:

  • 已经在这方面审查了几十个 SO 线程,你的 sn-p 很棒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2021-09-07
  • 2012-08-30
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
相关资源
最近更新 更多