2014-07-15 271 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

相关问题