2016-01-13 41 views
1

我用下面的代码列出所有与特定的分类名称和内容:动态分类项

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

    } 

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

+0

你在'$ CountryName'中有什么?它来自哪里? – Sumit

回答

1

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

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

+0

谢谢你!正是我需要的。 –

0

那么$ countryName其实就是帖子标题? 如果是这样的话:$ countryName = get_the_title();