2016-05-12 129 views
2

任何人都可以帮助解决这个问题。我需要限制发布在wp_query如何在wp_query中限制帖子

我知道如何限制后在wp_query

例如,如果该页面ID是20,我正在查询:

function wpcodex_filter_main_search_post_limits($limit, $query) { 

    if (get_the_ID()==20) { 
     return 'LIMIT 0, 12'; 
    } 

    return $limit; 
} 


add_filter('post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2); 

但我需要的LIMT设置为25〜35。

如何做到这一点?

即使我将返回'LIMIT 25, 35';仍呈现前35级的产品,而不是25之间的职位,以35

查询是这样的:LIMIT 10 OFFSET 15

请帮助我。

+0

看看这个https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination你需要使用偏移和分页每页帖子等etc ... –

+0

我看到'showposts'=> 2000,'post_status'\t \t =>'publish', 'offset'=> 11。但它不起作用。我看到这个。我可以更改抵消10,这是分页号码和showpost显示多少帖子显示。但我不知道它为什么不起作用。 – Oscar

回答

3

答案是... taratata !!!!

function wpcodex_filter_main_search_post_limits($limit, $query) { 
    if (get_the_ID()==20) { 
     return 'LIMIT 25, 10'; 
    } 
    return $limit; 
} 
add_filter('post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2); 

因为在'LIMIT 25, 10';,这里25是偏移量和10显示帖子的数量。所以,这样它会显示10个产品开始与产品26 ...

看到这个线程:Whats the difference between post_limits and pre_get_posts

+0

谢谢你的朋友:)。我会检查这一点。 – Oscar

+0

谢谢。这是工作 – Oscar