Hi All
I was looking at putting pagination on a standard page using the looper provider (query builder). As I understand, this it is not possible to use the std pagination X Theme provides unless you are on an archive or single layout.
With this issue in mind, and with a little help from AI, I have managed to build a function and shortcode to achieve what I required for my site. I thought i would share here. I know some of you hardened coders will be rolling your eyes at the use of AI, but I’m in the process of learning PHP as I come from a Cold Fusion background. So if there are some bits in this that need to be changed please chip in.
Ok for the function, will be best if you use a child theme for this and add this code to functions.php.
// Pagination for custom post types
function custom_looper_pagination($atts) {
$atts = shortcode_atts(array(
'post_type' => 'post',
'per_page' => 2,
), $atts, 'looper_pagination');
$paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
$args = array(
'post_type' => sanitize_key($atts['post_type']),
'posts_per_page' => intval($atts['per_page']),
'paged' => $paged,
);
$query = new WP_Query($args);
$total_pages = $query->max_num_pages;
wp_reset_postdata();
if ($total_pages <= 1) return '';
$output = '<ul class="page-numbers">';
for ($i = 1; $i <= $total_pages; $i++) {
// Use get_pagenum_link() to generate permalink-friendly URLs
$url = get_pagenum_link($i);
$class = 'page-numbers';
if ($i === $paged) {
$class .= ' current';
}
$output .= '<li><a class="' . esc_attr($class) . '" href="' . esc_url($url) . '">' . $i . '</a></li>';
}
$output .= '</ul>';
return $output;
}
add_shortcode('looper_pagination', 'custom_looper_pagination');
The shortcode on the page to go directly under you post listings (can be in another section)
[looper_pagination post_type=“your post type” per_page=“10”]
The post type here must match the query builder post type as well as the query builder page count.
You can also style the pagination by adding the following code to your page css in cornerstone.
/* style the custom pagination */
.page-numbers {
list-style: none;
padding: 0;
display: flex;
gap: 10px;
}
.page-numbers li a {
display: block;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
text-decoration: none;
color: #0073aa;
transition: background-color 0.3s, color 0.3s;
}
.page-numbers li a:hover {
background-color: #0073aa;
color: #fff;
}
.page-numbers li a.current,
.page-numbers li a.current:hover {
background-color: #005177;
color: #fff;
cursor: default;
pointer-events: none;
border-color: #005177;
}
That’s about it, hope it helps, this is really a basic form or pagination, but suited my needs. And i could not find anything else that helped me on the subject.