wordpress 中附件页面的分页

问题描述 投票:0回答:1

我有一段用于显示 WordPress 附件的代码。此代码显示页面中最新的 40 个附件,但我想为此代码添加分页。 代码:

    $args = array( 'post_type'  => 'attachment',
        'post_mime_type' => 'image',
             'post_status'    => 'inherit',
     'orderby' => 'date',
         'order' => 'DESC',
    'post__not_in'=>array($post->ID),
            'posts_per_page' => 40,
            'tax_query' => $tax_query );

    // finally run the query
    $loop = new WP_Query($args);

    if( $loop->have_posts() ) {

    ?>
          
<div class="masonry-items">
  <ul class="grid effect-2" id="grid">
    <?php while( $loop->have_posts() ) : $loop->the_post(); ?> 
           <li class="item">
      <div class="thumb">
      <a class="relative" href="<?php echo get_attachment_link( $image->ID ); ?>">
           <?php echo wp_get_attachment_image($image->ID, $size = 'thumbnail'); ?>
            <div class="caption absolute">
               <p>
                   <?php  echo wp_get_attachment_caption(get_post_thumbnail_id()); ?>
               </p>
             </div>
           </a>            
      </div>
    </li>
       <?php 
        endwhile;
    }
    wp_reset_query();
?>
php wordpress custom-wordpress-pages
1个回答
0
投票

您可以使用

the_posts_pagination
进行分页

<?php 
    $args = array( 'post_type'  => 'attachment',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'orderby' => 'date',
    'order' => 'DESC',
    'post__not_in'=>array($post->ID),
    'posts_per_page' => 1,
    'tax_query' => $tax_query 
);

// finally run the query
$loop = new WP_Query($args);

if( $loop->have_posts() ) {

?>
      
<div class="masonry-items">
  <ul class="grid effect-2" id="grid">
    <?php while( $loop->have_posts() ) : $loop->the_post(); ?> 
           <li class="item">
      <div class="thumb">
      <a class="relative" href="<?php echo get_attachment_link( $image->ID ); ?>">
           <?php echo wp_get_attachment_image($image->ID, $size = 'thumbnail'); ?>
            <div class="caption absolute">
               <p>
                   <?php  echo wp_get_attachment_caption(get_post_thumbnail_id()); ?>
               </p>
             </div>
           </a>            
      </div>
    </li>
       <?php 
        endwhile;
        $GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages;
        the_posts_pagination( array(
           'mid_size' => 1,
           'prev_text' => __( 'Back', 'green' ),
           'next_text' => __( 'next', 'green' ),
           'screen_reader_text' => __( 'Posts navigation' )
        ) );
    }

    wp_reset_query();
 exit;
© www.soinside.com 2019 - 2024. All rights reserved.