-1

I'm coding a foodblog and I'm trying to show favorite posts list in a specific page but I have some issue with pagination. I've tried different solutions searching in previous questions but nothing solve the problem. Thanks a lot

 <?php $my_favs = get_user_meta(get_current_user_id(), 'user_favs', true);

         $args = array(
                'post_type' => array('recipe', 'post'),
                'post__in'  => $my_favs,
                'post_author' => get_current_user_id(),
                'pagination'             => true,
                'posts_per_page'         => -1
            );

            $recipes = new WP_Query( $args );
 while ( $recipes->have_posts() ) : $recipes->the_post(); ?>

                        <a href="<?php the_permalink();?>">
                            <h2>
                              <?php $title = get_the_title();
                                echo wp_trim_words($title, 4);?> 
                            </h2>
                        </a>
                        <p>
                            <?php $content = get_the_content();
                             echo wp_trim_words($content, 9, "...");?> 
                        </p>

          <?php endwhile; // End of the loop.?>
            <div class="pagination">
                <?php 
                    echo paginate_links();
                ?>
            </div>


            <?php wp_reset_postdata(); ?>   
0

This is the pagination function I use. I'm not sure if the github link is the original credit, but its one i found. Add the following to your functions.php file:

/**
 * Creates Custom Pagination
 * @link https://gist.github.com/rgfx/755cfb71fd1732e4e7b7bdcbd4b6e4e3
 * @param  string $numpages  [description]
 * @param  string $pagerange [description]
 * @param  string $paged     [description]
 * @return html
 */
function custom_pagination($numpages = '', $pagerange = '', $paged='') {


if (empty($pagerange)) {
    $pagerange = 2;
}

/**
 * This first part of our function is a fallback
 * for custom pagination inside a regular loop that
 * uses the global $paged and global $wp_query variables.
 * 
 * It's good because we can now override default pagination
 * in our theme, and use this function in default quries
 * and custom queries.
 */
global $paged;
if (empty($paged)) {
    $paged = 1;
}
if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
            $numpages = 1;
    }
}

/** 
 * We construct the pagination arguments to enter into our paginate_links
 * function. 
 */
$pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __(''),
    'next_text'       => __(''),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
);

$paginate_links = paginate_links($pagination_args);

if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
        echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
        echo $paginate_links;
    echo "</nav>";
}

}'

Then, inside your archive template, add this right before your wp_reset_postdata(); :

<!-- pagination here -->
<?php if (function_exists(custom_pagination)) {
    custom_pagination($post_query->max_num_pages,"",$paged);
} ?>

Also, it may depend on what page template you are on. Im assuming a custom template or an archive page template.

Next, if youre still having trouble, then id suggest checking your $paged variable, like so:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$suri = $_SERVER['REQUEST_URI'];
    // check $paged variable
    echo '<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">';
    print_r($paged);
    echo '</pre>';
// grab the last number from the url to use as the $paged variable
$exp_suri = explode('/',$suri);
$page_query = $exp_suri[2];
$page_num = substr($page_query, -1);
$paged = is_numeric($page_num) ? $page_num : $paged;
    // Now, check $paged variable again
    echo '<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">';
    print_r($paged);
    echo '</pre>';

You can probably remove or comment out the second part ($exp_suri to the end) to begin. The pre tags here are display:none, so either take that out of the inline style, or show it using dev tools.

Finally, if youre still stuck, id read the WordPress pagination trouble shooting, here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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