3

假设我想使用 $wpdb 类从数据库中检索图像位置,这样我就可以创建一个图像库。我有这段代码,但是当我按“下一步”时,链接似乎没有去任何地方。我错过了什么吗?

<?php
global $wpdb;
$wpdb->show_errors();
$offset = 0;
if( isset($_GET['page']) && !empty($_GET['page']) ){
$offset =  ($_GET['page']-1) * 10; // (page 2 - 1)*10 = offset of 10
}
$pics = $wpdb->get_col("SELECT pic_thumb_url FROM wp3_bp_album 
WHERE owner_type = 'user' ORDER BY title DESC
LIMIT 10 OFFSET $offset"
);
//LIMIT shows 10 results per page
//OFFSET will 'skip' this number off results. On page 1 the offset is 0 on page 2 it is 10       (if 10 results per page)
foreach($pics as $pic) :
echo '<a href ="'. '#' .'" > <img src="' . $pic . '">' . '</a>';

endforeach; 


/*
pagination
*/
?>
<a href="/community/?page=<?php echo $_GET['page']-1 ?>">previous</a>
<a href="/community/?page=<?php echo $_GET['page']+1 ?>">next</a>

我可以用这个实现分页吗?

4

1 回答 1

3

您不能使用内置分页,但要使用分页,请在查询中使用 anOFFSET和 a 。LIMIT所以,制作自己的分页:

<?php
$offset = 0;
if(isset($_GET['page']) && !empty($_GET['page']) {
    $offset =  ($_GET['page']-1) * 10; // (page 2 - 1)*10 = offset of 10
}
$wpdb->get_col("SELECT pic_thumb_url FROM wp3_bp_album 
    WHERE owner_type = 'user' ORDER BY title DESC
    LIMIT 10 OFFSET $offset"
);
//LIMIT shows 10 results per page
//OFFSET will 'skip' this number off results. On page 1 the offset is 0 on page 2 it is 10 (if 10 results per page)

/*
pagination
*/
?>
<a href="/currentpage/?page=<?php echo $_GET['page']-1 ?>">previous</a>
<a href="/currentpage/?page=<?php echo $_GET['page']+1 ?>">next</a>

并非完美无缺,它不会检查您是在第一页还是最后一页,但至少是您必须自己构建的第一个检查。

于 2012-04-24T17:54:22.627 回答