1

I would like to have my single-{slug}.php perform a query on a list of posts from different custom post type and use pagination. The pagination appears but unfortunately, clicking the pagination does not go to that page and always remains on page 1. Currently using WP-navi, btw.

Example: Load the list of post from single-sports.php onto single-movies.php

Any advice or help would be appreciated.

Here's my single-{slug}.php:

<?php
         $args = array(
                       'post_type' => 'crew',
                       'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
                       );


    while (have_posts()) : the_post();
     /* Do whatever you want to do for every page... */
    ?>
        <div class="project_item">
            <div class="dotted">
              <hr />
            </div>
            <div class="project_thumb"><a href="<?php echo get_permalink(); ?>"><img src=""  /></a></div>
            <div class="project_entry"><h4><a href="<?php echo get_permalink(); ?>"></a></h4>

              <a href="<?php echo get_permalink(); ?>" class="readmore">Read more..</a> </div>
          </div>

          <?php

    endwhile;
    ?>
    <nav>
    <?php wp_pagenavi(array('query'=>$post_query)); ?>
    </nav>

    <?php
    wp_reset_query();  // Restore global post data
    ?>

Here's my functions.php:

$labels = array(
'name' => _x('Crew', 'post type general name'),
'singular_name' => _x('A crew', 'post type singular name'),
'add_new' => _x('Add A New Crew', 'crew'),
'add_new_item' => __('Add A New Creww'),
'edit_item' => __('Edit Crew'),
'new_item' => __('New Crew'),
'view_item' => __('View Crew'),
'search_items' => __('Search Cre'),
'not_found' => __('Crew Not Found'),
'not_found_in_trash' => __('Not found in trash'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'menu_position' => 6,
'public' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
);

register_post_type('crew',$args);

$args = array(
'label' => 'Author Catefory',
'public' => true,
'show_ui' => true,
'hierarchical' => true
);
register_taxonomy('crew_tax','crew',$args);
2

You forgot to start a new query.

And if you dont use have post on your custom query, you will get the main current query, in your case is your single.

$args = array(
       'post_type' => 'crew',
       'posts_per_page' => 10
       );

$your_custom_query = new WP_Query( $args ); // is that you forgot

// The Loop
if ( $your_custom_query->have_posts() ) :
while ( $your_custom_query->have_posts() ) : $your_custom_query->the_post();
  // Do Stuff
endwhile;
endif;

i dont know what is wp_pagenavi()

use paginate_links()

$big = 999999999; // need an unlikely integer
$pagination_args = array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $your_custom_query->max_num_pages,
            'type' => 'plain',
            'prev_text' => 'Prev',
            'next_text' => 'Next'
        );

echo(  paginate_links( $pagination_args )  );

And finish by a reset

wp_reset_postdata();

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.