Support » Developing with WordPress » How to make an “original archive page” where pagination can be fi

  • Even if you use custom post type, when you load “post” in the main loop, the template becomes archive.php, and when you load it in a subloop, the page number is not displayed.

    —-archive-yyyy2.php—-

    <?php
    /**
     * The template for displaying archive pages
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
     *
     * @package WordPress
     * @subpackage Twenty_Nineteen
     * @since 1.0.0
     */
    
    get_header();
    ?>
    
                <?php  $paged = get_query_var('page') ? get_query_var('page') : 1;     
                $args = array( 'posts_per_page' => 2,
                               'paged' => $paged,
                             'nopaging' => false ); 
                $the_query = new WP_Query($args);
                         $n=1; 
                ?>
               
                <div class='mypostlist'> 
    <?php  $max_num_pages = $the_query->max_num_pages; ?>
                <?php  if($the_query -> have_posts()) : while($the_query -> have_posts()) : $the_query -> the_post();
                        ?>
                  <div class="aida1">  
                <?php the_title(); ?><br>
                <?php echo $n ;?>                   
                <?php if(has_post_thumbnail()): ?>
                   <a href="<?php the_permalink() ?>"> 
    
                          <?php the_post_thumbnail('index_thumbnail',array('class'=>'forsize')); ?>
                      </a>
    
                <?php else: ?>
                  <a href="<?php the_permalink() ?>"> 
                       <img src="<?php  echo get_stylesheet_directory_uri(); ?>/no-image.png" class="forsize" >
                    </a>
                <?php endif; ?>
                <?php $n++; ?>
                <br>
                <dl>
                <dt>
                <?php the_time('Y.m.d') ?> 
            
                </dt>
                </dl>
    
                  </div>     
                
        <?php endwhile; ?>
                    <?php else: ?>    
                    <p>投稿がありません</p>
                    <?php endif; ?>
                 </div><!--mypostlink-->
    
       
     
    		
    <?php wp_reset_postdata(); ?> 		
    		
    <?php the_posts_pagination(array(
    	'mid_size' => 2,
    	'prev_text' => __( 'ooo' ),
    	'next_text' => __( 'eee' ),) ); ?>
     
    <?php
    get_footer(); ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • When using the main loop instead of the subloop
    Description of function.php

    function twpp_change_sort_order2( $query ) {
      if ( is_admin() || ! $query->is_main_query() ) {
        return;
      }
    
      if ( $query->is_post_type_archive('yyyy2') ) {
        $query->set( 'posts_per_page',1);
        $query->set( 'post_type',array('post','yyyy2'));                                 }
    }
    
    add_action( 'pre_get_posts', 'twpp_change_sort_order2' );
    Moderator bcworkz

    (@bcworkz)

    the_posts_pagination() will not work with custom queries instantiated on templates. It relies upon global variables related to the main query used for the original request. To paginate custom queries, use paginate_links().

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.