Let’s say I want to use the $wpdb class to retrieve image locations from the database so I can then create a gallery of images. I have this code, but when I press ‘next’, the link doesn’t seem to go anywhere. Am I missing something?
<?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>
Can I implement pagination with this?
You can’t use the built-in pagination, but to use pagination use an
OFFSETand aLIMITin your query. So, make your own pagination:Not flawless, it doesn’t check if you are on the first or last page but at least the first check you have to build yourself.