2016-01-13 38 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();