Support » Plugin: WP-PageNavi » How to call wp_pagenavi() in a shortcode which uses custom post types

  • Hello,

    I am not able to figure out how to call pagination for a custom post type which outputs from a shortcode.

    In my functions.php file short-code, I have the below

      <?php
    
    function casestudy_list_func($atts) {
        $output = '';
        extract(shortcode_atts(array(
            'title' => '',
            'totalposts' => '-1',
            'speciality' => '',
            'order' => 'DESC'
        ), $atts));
    
        $output. = '<div class="blog_info"><div class="blog_list">';
        ob_start();
        global $post;
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        $args = array(
            'posts_per_page' => 10,
            'order' => $order,
            'paged' => $paged,
            'post_type' => 'casestudy',
        );
        $myposts = NEW WP_Query($args);
        $max_num_pages = $myposts - > max_num_pages;
        while ($myposts - > have_posts()) {
            $myposts - > the_post();
            $next_link = get_next_posts_link('Older Case Studies', $max_num_pages);
            $previous_link = get_previous_posts_link('Newer Case Studies', $max_num_pages);
            
            $output. = '<article>';
            // content loop goes here
            $output. = '</article>';
        }
        $output. = '<div class="test">'.wp_pagenavi(). '</div>';
        $output. = '<div class="testimonial_nav"><div class="nav-previous">'.$next_link.'</div>';
        $output. = '<div class="nav-next">'.$previous_link.'</div></div>';
    
        $output. = '</div></div>';
        wp_reset_query();
        ob_end_clean();
        return $output;
    
    }
    add_shortcode('case-studies', 'casestudy_list_func');

    In the div, I don’t get any output, whereas I get Older Case Studies, newer Case Studies links as pagination

    I tried the below as well. but no luck
    wp_pagenavi(array('query' => $myposts));

    Can anyone tell me where am I doing wrong or missing anything?

  • You must be logged in to reply to this topic.