如何不在页面中重复相同的最近帖子两次?

时间:2017-01-22 18:57:42

标签: php wordpress

function getPostsbyCategoryID($category_id) {     
    $category_posts = wp_get_recent_posts(array('numberposts' => '3','category' => $category_id, 'post_status' => 'publish'), ARRAY_A );  
    $i = 0;   
    foreach($category_posts as $p) {    
        $i++;         
        echo '<li>';    
        echo '</li>';    
    }     
    wp_reset_query(); 
}

这是我从类别中获取帖子的查询,  我在用  用于显示帖子。  我想删除重复的最近帖子,检查代码是否有任何错误回复我,谢谢

1 个答案:

答案 0 :(得分:0)

我不是WP专家,但我可以read a manual

您可以在此调用中传递一个偏移参数,这样可以跳过第一个(x)帖子

function getPostsbyCategoryID($category_id) {  

    $args = array('numberposts' => 3,
                  'offset' => 3,                    // <-- new line
                  'category' => $category_id, 
                  'post_status' => 'publish')    
    $category_posts = wp_get_recent_posts($args, , ARRAY_A );

    $i = 0;   
    foreach($category_posts as $p) {    
        $i++;         
        echo '<li>';    
        echo '</li>';    
    }     
    wp_reset_query(); 
}