I have made the shortcode which displays data from database. There are so many rows and so I made pagination. But I can't get the URL param which means page number.
<?php
/**
* Plugin Name: Plugin hell
*/
if (!defined('ABSPATH')) {
die;
}
function count_rows()
{
global $wpdb;
$table_name = $wpdb->prefix . 'strong_concordance';
$count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
return intval($count);
}
function get_data($offset, $per_page)
{
global $wpdb;
$table_name = $wpdb->prefix . 'strong_concordance';
$data = $wpdb->get_results("SELECT * FROM $table_name LIMIT $offset, $per_page");
return $data;
}
function display_data_shortcode($atts)
{
$atts = shortcode_atts(
array(
'page' => 1,
'per_page' => 10,
), $atts, 'display_data');
//$page = intval($atts['page']);
$page = (get_query_var('page')) ? get_query_var('page') : 1;
var_dump($page);
$per_page = intval($atts['per_page']) ? : 10;
$num_pages = ceil(count_rows() / $per_page);
$offset = ($page - 1) * $per_page;
$data = get_data($offset, $per_page);
$output = '';
foreach ($data as $row) {
$output .= '<h3>' . $row->strong_reference . '</h3>';
$output .= '<p style="font-weight: bold;">' . $row->greek_word . '</p>';
$output .= '<p>' . $row->explanation . '</p>';
}
if ($page > 1) {
$output .= '<a href="?page=' . ($page - 1) . '">Previous</a>';
}
for ($i = 1; $i <= $num_pages; $i++) {
$output .= '<a href="?page=' . $i . '">' . ' ' . $i . ' ' . '</a>';
}
if ($page < $num_pages) {
$output .= '<a href="?page=' . ($page + 1) . '">Next</a>';
}
return $output;
}
add_shortcode('display_data', 'display_data_shortcode');
?>
There will be page numbers like "1 2 3 4 5 ... ". I click any number-for example 4, the $page should be 4 and so $offset should be 30, but the $page is still 1 and so there will be same data. How can I solve this issue?