假设我想使用$ 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>
我可以用它来实现分页吗?
答案 0 :(得分:3)
您无法使用内置分页,但要使用分页,请在查询中使用OFFSET
和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>
没有完美,它不会检查你是在第一页还是最后一页,但至少是第一次检查你必须自己建立。