programing

WordPress paginate_links - 사용방법

bestprogram 2023. 2. 26. 16:24

WordPress paginate_links - 사용방법

하위 페이지가 있는 페이지에 번호 페이지를 추가하고 싶습니다.(부트스트랩에서) 작성하는 페이지 수는 다음과 같습니다.

<nav>
  <ul class="pagination">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

그리고 이게 내 코드야

<!-- block -->
<div class="row grid events-block">

    <?php
    $parent = $post->ID;
    // query_posts('posts_per_page=15&post_type=page&post_parent='.$parent);
    query_posts(array('posts_per_page'=>'1', 'post_type' => 'page', 'post_parent' => $parent, 'paged' => get_query_var('paged')));
        while (have_posts()) : the_post();
    ?>

    <!-- item -->
    <div class="grid-item col-md-6 col-sm-6 col-xs-12 event-item">

        <div class="date-box">
            <?php echo $event_dates; ?>
        </div>

        <div class="event-item-text-box">
            <div class="event-item-text-inner-box">
                <h3 class="heading-item"><a href="#" target="_blank"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
        </div>

    </div>
    <!-- item -->
     <?php endwhile; ?>

     <?php
     // Reset the post to the original after loop. otherwise the current page becomes the last item from the while loop.
     // https://codex.wordpress.org/Function_Reference/wp_reset_query
     wp_reset_query();
     ?>

</div>
<!-- block -->

<?php $args = array(
    'base'               => '%_%',
    'format'             => '?paged=%#%',
    'total'              => 1,
    'current'            => 0,
    'show_all'           => false,
    'end_size'           => 1,
    'mid_size'           => 2,
    'prev_next'          => true,
    'prev_text'          => __('« Previous'),
    'next_text'          => __('Next »'),
    'type'               => 'plain',
    'add_args'           => false,
    'add_fragment'       => '',
    'before_page_number' => '',
    'after_page_number'  => ''
); ?>

<?php echo paginate_links( $args ); ?>

그런데 작동이 안 돼요.내가 뭘 놓쳤는지 알아?

페이지 번호부여: Prev 1 2 3 Next

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$data= new WP_Query(array(
    'post_type'=>'YOUR_POST_TYPE', // your post type name
    'posts_per_page' => 3, // post per page
    'paged' => $paged,
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();
            // Your code
    endwhile;

    $total_pages = $data->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

위의 코드를 사용해 주시겠습니까?당신에게 도움이 될 것 같아요.

여기 멋진 부트스트랩 페이지링크를 갖는 기능이 있습니다.

function bootstrap_pagination()
{
    global $wp_query;
    $big = 999999999;
    $listString = paginate_links(array(
        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_query->max_num_pages,
        'prev_text'    => __('← Previous'),
        'next_text'    => __('Next  →'),
        'type'  => 'list',

    ));
    
    $listString = str_replace("<ul class='page-numbers'>", '<ul class="pagination">', $listString);
    $listString = str_replace('page-numbers', 'page-link', $listString);
    $listString = str_replace('<li>', '<li class="page-item">', $listString);
    $listString = str_replace(
        '<li class="page-item"><span aria-current="page" class="page-link current">',
        '<li class="page-item active"><span aria-current="page" class="page-link">',
        $listString
    );


    echo $listString;

}

언급URL : https://stackoverflow.com/questions/38939212/wordpress-paginate-links-how-to-use-it