Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pass extra parameter in Wordpress pagination?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.77k
    Comment on it

    Hello readers, today I guide you "How to pass extra parameter in Wordpress pagination?".

     

    Recently I am working on a project and I want to pass extra parameter in Wordpress paged pagination. Normally we are are using simple pagination for break data into fixed number of post shown in at a time.

    In below code, categories are shown in dropdown box and when we select any category, their related post are shown with pagination. When we click next on pagination, page load and nothing will show this is the reason why I pass extra parameter in pagination.

    Use the below code for pass extra parameter,

     

    <?php
    function list_post($cat_id=4){
        global $wpdb;
        if($cat_id == $_GET['cat']){
            $cat_id = $_GET['cat'];
        }else{
            $cat_id;
        }
        $paged = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
        $args = array(
            'post_type' => 'post',
            'posts_per_page'=>2,
            'paged' => $paged,
            'tax_query' => array(
                array(
                'taxonomy' => 'category',
                'terms' => $cat_id
                )
              )
            );
    
        $query = new WP_Query( $args );
        //echo "<pre>"; print_r($query);
        echo '<table border="1" width="70%" align="center">';
        echo '<tr>';
        echo '<th>S No.</th>';
        echo '<th>Title</th>';
        echo '<th>Content</th>';
        echo '<th>Action</th>';
        echo '</tr>';
        $j=1;
        if($query->have_posts()){
            while ($query->have_posts()){
                $query->the_post();
                $post_id = get_the_ID();
                $edit_link = admin_url("admin.php?page=edit-post&amp;&amp;post_id=$post_id");
                $delete_link = admin_url("admin.php?page=get_all_category&amp;action=delete&amp;post_id={$post_id}");
        ?>
            <tr>
                <td width="10%" align="center"><?=$j;?></td>
                <td width="20%" align="center"><?=the_title();?></td>
                <td width="60%" align="center"><?=the_content();?></td>
                <td width="10%" align="center">
                    <a href="<?=$edit_link;?>">Edit</a> |
                    <a href="javascript:void(0)" onclick="deletePost('<?=$delete_link; ?>')">Delete</a>
                </td>
            </tr>
        <?php
                $j++;
                }
            }
        ?>
        </table>
        <div class="pagination">
            <?php
                $big = 999999999; // need an unlikely integer
                $search_for   = array( $big, '#038;' );
                $replace_with = array( '%#%', '&' );            
                echo paginate_links( array(
                    'total' => $query->max_num_pages,
                    'current' => max( 1, get_query_var('paged') ),
                    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format' => '?paged=%#%',   
                    'add_args' => array( 'cat' => $cat_id ),
                ) );
            ?>
        </div>
    <?php
    }

     

    If you use the above code, also use the below function. This function replace "#038" to "&", put this code into functions.php file.

    function amp_url_bugfix($link) {
    return str_replace('#038;', '&', $link);
    }
    add_filter('paginate_links', 'amp_url_bugfix');

     

    That's it, I hope it helps you!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: