plugins – How to get URL param for pagination in shortcode?

Question

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']);

    $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, but it still displays same.
How can I solve this issue?

0
Rocky 1 month 2022-12-26T02:16:08-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse