bogdanbatsenko

CPT archive pagination

Oct 18th, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /* archive-illustration.php CPT archive pagination*/
  2.  
  3. <?php /*
  4. Template Name: Illustrations
  5. */
  6. get_header(); ?>
  7.  
  8. <div class="page">
  9.  
  10. <h1 class="page-title">I Draw</h1>
  11.  
  12. <?php
  13. $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  14. $args = array(
  15. 'post_type' => 'illustration',
  16. 'post_status' => 'publish',
  17. 'posts_per_page' => 3,
  18. // 'orderby' => 'title',
  19. // 'order' => 'ASC',
  20. 'paged' => $paged,
  21. //'paged' => get_query_var('paged', 1) // страница пагинации
  22. ); ?>
  23.  
  24. <!-- pagination here -->
  25. <!-- the loop -->
  26. <?php $archive_loop = new WP_Query( $args ); ?>
  27. <div class="container">
  28. <?php if($archive_loop->have_posts()) : ?>
  29. <?php while ( $archive_loop->have_posts() ) : $archive_loop->the_post(); ?>
  30.  
  31. <article class="illustration">
  32. <a href="<?php the_permalink(); ?>">
  33. <h2 class="illustration__title"><?php the_title(); ?></h2>
  34. </a>
  35. <a href="<?php echo the_post_thumbnail_url( 'full' ); ?>"
  36. title="<?php echo the_title(); ?>">
  37. <img
  38. src="<?php the_post_thumbnail_url( 'illustration_big' ); ?>"
  39. srcset="<?php the_post_thumbnail_url( 'illustration_small' ); ?> 500w,
  40. <?php the_post_thumbnail_url( 'illustration_big' ); ?> 1000w, "
  41. sizes="100vw"
  42. alt="<?php echo the_title_attribute(); ?>"
  43. >
  44. </a>
  45. <div class="illustration__date"><?php echo get_the_date('d/m/y'); ?></div>
  46. </article>
  47. <?php endwhile;
  48. ?>
  49. <!-- end of the loop -->
  50.  
  51.  
  52. <div class="pagination">
  53. <?php $total_pages = $archive_loop->max_num_pages;
  54. if ($total_pages > 1){ ?>
  55. <div class="archive-pagination">
  56. <?php $current_page = max(1, get_query_var('paged'));
  57.  
  58. echo paginate_links(array(
  59. //'mid_size' => 2,
  60. 'base' => get_pagenum_link( 1) . '%_%',
  61. 'format' => 'page/%#%',
  62. 'current' => $current_page,
  63. 'total' => $total_pages,
  64. 'prev_text' => __('« prev'),
  65. 'next_text' => __('next »'),
  66. ));
  67. ?>
  68. </div>
  69. <?php }
  70. wp_reset_postdata();
  71. ?>
  72. </div>
  73. </div><!-- container -->
  74. <?php endif; ?>
  75.  
  76. </div><!-- page -->
  77.  
  78. <?php get_footer(); ?>
  79.  
  80.  
  81.  
  82. /* functions */
  83. //https://wordpress.stackexchange.com/questions/158532/how-to-make-blog-pages-show-at-most-setting-not-affect-custom-queries
  84. function set_my_pagination( $query )
  85. {
  86. if ( is_admin() || !$query->is_main_query() )
  87. return;
  88.  
  89. if ( is_home() )
  90. {
  91. $query->set( 'posts_per_page', 5 );
  92. return;
  93. }
  94. if ( is_post_type_archive( 'illustration' ) )
  95. {
  96. $query->set( 'posts_per_page', 2 );
  97. return;
  98. }
  99.  
  100. }
  101. add_action( 'pre_get_posts', 'set_my_pagination', 1 );
Add Comment
Please, Sign In to add comment