2013-03-26 74 views
0

我想通过使用它的slug名称(例如'最受欢迎')来获取所有职位在一个类别下。我该怎么做呢?得到一个类别的所有职位id按类别slug

这是我试过的,但失败了。

<?php 
        global $post; 
        $id = array(); 
        $i=0; 
        $args = array('numberposts' => 5, 'category' => "most-popular", 'post_status' => "publish", 'order'=>"ASC"); 
        $myposts = get_posts($args); 
        foreach($myposts as $post) : 
         setup_postdata($post); 
         $id[$i]=$post->ID; 
         $i=$i+1; 
        endforeach; 

        $article1= new Article($id[0]); 
              $article2= new Article($id[1]); //and so on. 
       ?> 

回答

1

你很可能需要通过调用get_category_by_slug($slug)得到id第一。所以也许是这样的:

$category = get_category_by_slug('most-popular'); 
$args = array('numberposts' => 5, 'category' => $category->term_id, 'post_status' => "publish", 'order'=>"ASC");