动态分类术语

Dynamic Taxonomy term

我正在使用以下代码列出具有特定分类法名称的所有内容:

$myposts = get_posts(array(
    'showposts' => -3,
    'post_type' => 'post',
    'tax_query' => array(
        array(
        'taxonomy' => 'country',
        'field' => 'slug',
        'terms' => array('Egypt'))
    ))
);

foreach ($myposts as $mypost) {
      echo $mypost->post_title . '<br/>';

}

我想根据 PHP 调用动态输入 "terms" 名称,如下所示:

$CountryName = echo the_title();
$myposts = get_posts(array(
        'showposts' => -3,
        'post_type' => 'post',
        'tax_query' => array(
            array(
            'taxonomy' => 'country',
            'field' => 'slug',
            'terms' => array($CountryName))
        ))
    );

    foreach ($myposts as $mypost) {
          echo $mypost->post_title . '<br/>';

    }

但是,语法当然是错误的。我该怎么做?谢谢!

所以 $countryName 实际上是 post 标题? 如果是:$countryName = get_the_title();

$CountryName = echo the_title(); 不是为变量赋值的正确方法。

在这种情况下,如果你想使用外部变量,你必须这样做 $CountryName = get_the_title();。或者在查询中使用 get_the_title()