2014-07-15 268 views
-1

我想運行一個循環,只查詢我的WordPress的特定類別。這是我有,但它顯示的「推薦」類別在wordpress中做一個特定的帖子類別顯示

<div class="row"> 
      <?php if (is_page()) { 
      $cat=get_cat_ID($post->post_title); //use page title to get a category ID 
      $posts = get_posts ('cat=$cat&showposts=5'); 
      if ($posts) { 
       foreach ($posts as $post): 
       setup_postdata($post); ?> 

        <div class="col-sm-12"> 
        <div class="box" data-toggle="modal" data-target="#myModal1"> 
         <h1><?php the_title(); ?></h1> 
         <?php the_content(); ?> 
        </div> 
        </div> 

       <?php endforeach; 
      } 
      }?> 
     </div> 

請任何幫助將是巨大的在所有我的職務,而不是隻是我的帖子, 日Thnx

回答

2

那是因爲參數發送到get_posts是非法的。嘗試:

$posts = get_posts(array(
    'category'=>$cat, 
    'posts_per_page'=>5, //This changes how many posts are returned 
    'offset' => 0, //This changes where it search starts from 
); 

看一看在documentation here

還有一種更好的方式來得到這個職位類別:

$cat = get_the_category(); 

你不必帖子ID傳給它,因爲它將默認爲當前$post->ID。 您可以檢出該documentation here

它也可能是一個壞主意,用$post在你的foreach循環爲將覆蓋global $post變量。您可能想要使用類似$curPost

相關問題