对$wpdb中的结果集进行分页->Get_Results()

时间:2010-10-26 作者:anu

是否有一种简单的方法可以对来自$wpdb->get\\u results()的结果集分页?

我想在author\\u archive页面上获得用户评论的分页列表-社区中的一些用户的评论超过500条,因此分页很重要。

WordPress有没有内置的方法来实现这一点,或者我需要自己构建它?

[更新以添加代码]

<!-- Show comments -->
<?php 
$querystr = "
    SELECT comment_ID, comment_post_ID, post_title, LEFT(comment_content,100) as comment_content, comment_date_gmt
    FROM $wpdb->comments, $wpdb->posts
    WHERE user_id = $thisauthor->ID
    AND comment_post_id = ID
    AND comment_approved = 1
    ORDER BY comment_ID DESC
    LIMIT 100
 ";


$comments_array = $wpdb->get_results($querystr, OBJECT);

if ($comments_array): ?>
    <div id="author-comments-list">
        <h2>Recent Comments </h2>
        <ul>
            <? 
                $odd_even = "odd";
                foreach ($comments_array as $comment):
                    echo "<li class=\'$odd_even\'><a href=\'". get_bloginfo(\'url\') ."/?p=".$comment->comment_post_ID."/#comment-". $comment->comment_ID ."\'>". $comment->post_title ."</a> : ".mysql2date(\'l jS F, Y, g:ia\',$comment->comment_date_gmt);
                    echo "<div class=\'author-comments-content\'>".$comment->comment_content."</li>";
                    $odd_even = ($odd_even == "odd" ? "even" : "odd");
               endforeach; ?>
        </ul>
    </div> 
<? endif; ?>

2 个回复
最合适的回答,由SO网友:sorich87 整理而成

您可以使用该功能paginate_links() 对于任何分页。

针对您的具体情况:

$total = $wpdb->get_var("
    SELECT COUNT(comment_ID)
    FROM $wpdb->comments
    WHERE user_id = $thisauthor->ID
    AND comment_post_id = ID
    AND comment_approved = 1
");
$comments_per_page = 100;
$page = isset( $_GET[\'cpage\'] ) ? abs( (int) $_GET[\'cpage\'] ) : 1;

echo paginate_links( array(
    \'base\' => add_query_arg( \'cpage\', \'%#%\' ),
    \'format\' => \'\',
    \'prev_text\' => __(\'&laquo;\'),
    \'next_text\' => __(\'&raquo;\'),
    \'total\' => ceil($total / $comments_per_page),
    \'current\' => $page
));

SO网友:Mohammed Sufian

对于那些根据问题和@sorich87答案寻找完整答案的人。

我想我应该在这里发布解决方案,适合像我这样的人(wordpress初学者)。

//use in your custom page or custom post template 
global $wpdb;
$per_page = 5;
$page = isset( $_GET[\'cpage\'] ) ? abs( (int) $_GET[\'cpage\'] ) : 1;
if ($page > 1) {
    $offset = $page * $per_page - $per_page;
} else {
    $offset = $page;
}
$the_post = "select id,post_title from $wpdb->posts where post_type in (\'buy\',\'rent\') and post_status=\'publish\' "
        . "order by id desc limit $per_page offset $offset";
$details = $wpdb->get_results($the_post, OBJECT);
//do foreach to display your post details.


//paste this, where you want to display the pagination
$total = $wpdb->get_var("SELECT count(id) from $wpdb->posts where post_type in (\'buy\',\'rent\') and post_status=\'publish\' order by id desc");
echo paginate_links(array(
    \'base\' => add_query_arg(\'cpage\', \'%#%\'),
    \'format\' => \'\',
    \'prev_text\' => __(\'&laquo;\'),
    \'next_text\' => __(\'&raquo;\'),
    \'total\' => ceil($total / $per_page),
    \'current\' => $page
));
这就是我为WordPress网站工作的方式

结束

相关推荐

I can't view or add comments

嘿,那里。我刚刚建立了这个网站。http://www.paledogstudios.com 它工作得很好,只是我似乎看不到过去的评论(这个博客是从blogger导入的)或添加评论。我知道这是代码而不是设置,因为有人告诉我,但他没有进一步帮助我。他说这可能在索引上。php帮助