按类别slug获取所有类别的帖子ID

时间:2013-03-26 13:39:54

标签: php wordpress

我想通过使用其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 个答案:

答案 0 :(得分:1)

您可能必须先致电id获取get_category_by_slug( $slug )。也许是这样的:

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