WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom post type page template pagination (5 posts)

  1. Khashayarp
    Member
    Posted 1 year ago #

    I am trying to make a page template for the latest custom post types and it shows the recent 10 posts but i want to add a pagination at the end of it!
    it is not the archive page it is a custom page template.

    <?php
    global $wp_query;
    
    $wp_query = new WP_Query("post_type=questions&post_status=publish&posts_per_page=10");
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div style="margin:10px;padding:5px;border-bottom:1px dashed #ebebeb;height:auto;">
    <div style="float:right;width:60px;margin-left:10px;" class="questions-page-2">
    <?php echo get_avatar( get_the_author_email(), '48' ); ?>
    </div>
    <div style="float:left;width:450px;" class="questions-page-1">
    <h6 class="toggle"><a href="#" style="opacity: 1;"><?php the_title(); ?></a></h6>
    
    <div class="toggle_content" style="display: block;">
    <div class="block"><?php $content = get_the_excerpt();
    echo substr($content, 0, 120); ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">... show question</a>
    </div>
    
    </div>
    </div>
    <div class="clearboth"></div>
    </div>
    <?php endwhile;
    endif; ?>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

  3. Khashayarp
    Member
    Posted 1 year ago #

    it adds the pagination but when i click next page the content is same on every page!

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    have you added the 'paged' parameter to the query args?

    example:

    $wp_query = new WP_Query("post_type=questions&post_status=publish&posts_per_page=10&paged=".get_query_var('paged'));
  5. Khashayarp
    Member
    Posted 1 year ago #

    Thank you , i figured it out!

    in case if anybody faced this problem you could use this code :

    $wp_query = new WP_Query( array ( 'post_type' => 'questions', 'posts_per_page' => '5', 'paged' => get_query_var( 'paged' ) ) );

    and after the loop ends add this code :

    <?php posts_nav_link(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic