动态分类术语

时间:2016-01-13 14:59:00

标签: php wordpress taxonomy

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

$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调用动态地设置“术语”名称,如下所示:

$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/>';

    }

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

2 个答案:

答案 0 :(得分:1)

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

在这种情况下,如果要使用外部变量,则必须执行$CountryName = get_the_title();。或者只是在查询中使用get_the_title()

答案 1 :(得分:0)

所以$ countryName实际上是帖子标题? 如果是这样的话:$ countryName = get_the_title();

相关问题