0

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?

1 Answer 1

0

You can use get_query_var('paged'), this will return the current page in pagination.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Or the shorter form

$paged = get_query_var('paged') ?: 1;
8
  • Thanks for your answer. Where should I add these?
    – Rocky
    Commented Dec 26, 2022 at 7:30
  • Where you need the paged value Commented Dec 26, 2022 at 7:32
  • I added it in the function display_data_shortcode, but I can't get the page number.
    – Rocky
    Commented Dec 26, 2022 at 7:44
  • You'll need to update your code so I can see where you inserted the code. Also, how do you know it doesn't work? have you tried using a debug function to see its value? print_r, var_dump, or var_export should help with that Commented Dec 26, 2022 at 9:17
  • I really give appreciate to your help. I have just edited the code. function display_data_shortcode()
    – Rocky
    Commented Dec 26, 2022 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.