显示所有粘滞帖子,并进行分页

时间:2012-05-05 作者:Carmine Lucarelli

正在尝试获取适用于自定义帖子类型的自定义管理链接。我已经设法添加了一个链接,在通常的“all”、“Published”、“Trash”等旁边显示所有“粘性”帖子,但实际的查询并没有像我希望的那样工作。

在pre\\u get\\u posts中,我试图修改查询以执行以下操作:-仅显示粘性帖子-尊重posts\\u per\\u页面和分页设置

如果我尝试这样做:

$query->set(\'post__in\',  get_option(\'sticky_posts\'));
$query->set(\'posts_per_page\', 5);
。。。我看到了所有粘帖,但没有分页。设置“posts\\uu in”是否意味着忽略每页的posts\\u?

谢谢你的信息。放弃了“每页帖子数”,但这并没有什么不同。我的调试栏显示:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (2382,2358,2334,2331,2301,2298,2280,2262,2220,2226,2193,2157,2163,2136,2130,2109,2100,2084,2072,2026,2020,1961,1934,1846,1834,1756,1738,1709,1688,1552,1543,1498,1488,1482,1476,1438,1432,1412,1406,1400,1394,1391,1388,1358,1340,1319,1285,1282,1269,1227,1212,2621,2674,2659,3300,3313,3414,3560,3314) AND wp_posts.post_type = \'ad_listing\' AND (wp_posts.post_status = \'publish\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC 

1 个回复
SO网友:kevin

基于此post, 我做了这个测试并开始工作:

$sticky = get_option( \'sticky_posts\' );
$ppp = get_option(\'posts_per_page\');

if (!is_paged()) {
    $custom_offset = 0;
} else {
    $custom_offset = $ppp*($paged-1);
}

$args = array(
    \'numberposts\' => $ppp,
    \'offset\' => $custom_offset,
    \'post__in\' => $sticky 
);

$posts_data = get_posts( $args );
$pd = count( $posts_data );

if ( count( $posts_data ) > 0 ) {
    echo \'<ul>\';
    foreach ( $posts_data as $post ) {
        echo \'<li><a href="\'.get_permalink( $post->ID ).\'">\'.$post->post_title.\'</a></li>\';
    }
    echo \'</ul>\';
} 

if ( $pd != 1 ) { next_posts_link( __( \'<span class="meta-nav">&laquo;</span> P&aacute;gina anterior \', \'twentyten\' ) ); }
previous_posts_link( __( \'Pr&oacute;xima p&aacute;gina <span class="meta-nav">&raquo;</span>\', \'twentyten\' ) );
我将读取参数设置为2和4进行了测试,结果似乎正常。看看它是否适合你的需要。

结束

相关推荐