Xin code phân trang trong wordpress dễ sử dụng

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 30/10/22.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,028
    Đã được thích:
    80
    Điểm thành tích:
    48
    Chào mọi người, em có cái web, em đang tải lên tất cả bài viết của 1 post_type em tự tạo là Hỏi đáp (hoidap), em có tải nó ra 1 trang page mới hoàn toàn taxonomy teamplate hết rồi, nhưng tới phần phân trang lại không làm được, bạn làm biết giúp mình với.

    Em gửi cái code tải ra toàn bộ bài viết của cái hoidap post_type em tạo ra, nó tải ra được hết rồi, còn phần phân trang thì chưa làm được. cho em xin code em cảm ơn.

    PHP:
    <div class="large-9 col">
        <?php
            $paged 
    = (get_query_var('paged')) ? get_query_var('paged') : 1;
            
    $args =  array(
                
    'post_type' => 'hoidap',
                
    'paged' => $paged,
                
    'order' => 'DECS',
                
    'posts_per_page' => 5,
                
    'orderby'=>'date'
            
    );
            
    $loop = new WP_Query($args);
            
    $i 1;
            if(
    $loop->have_posts()) {
                while (
    $loop->have_posts()) {
                    
    $loop->the_post();
                    
    ?>
                    <div class="csq-item">
                        <div class="csq-title-item">
                            <span class="csq-number"><?php echo str_pad($i2'0'STR_PAD_LEFT); ?></span>
                            <p class="csq-title">Khách hỏi: <?php echo get_field('question'?></p>
                        </div>
                        <div class="csq-question">
                            <span>Câu hỏi:</span>
                            <h3><?php echo get_field('question'?></h3>
                        </div>
                        <div class="csq-anwser">
                            <label>Trả lời:</label>
                            <p>Chuyên gia trả lời: <?php echo get_field('name_author'?></p>
                            <div class="csq-content">
                                <?php the_content() ?>
                            </div>
                        </div>
                    </div>
                    <?php
                    $i
    ++;
                }
            }
        
    ?>
        <?php if (  $loop->max_num_pages ):
            
    ?>
            <nav class="navigation">
                <?php
                  
    ////code phân trang nằm ở đây, cho em xin code, em cảm ơn.
                
    ?>
            </nav>
        <?php endif; ?>
    </div>
     
    Cảm ơn đã xem bài:

    Xin code phân trang trong wordpress dễ sử dụng

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,895
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Demo đây:

    demo-phan-trang.png
    Code mặc định:

    Code phân trang mặc đinh đây nhé: Chèn code sau vào tập tin function.php của themes nhé.
    PHP:
    //Code phan trang
    function wp_corenavi_table($custom_query null) {
          global 
    $wp_query;
          if(
    $custom_query$main_query $custom_query;
          else 
    $main_query $wp_query;
          
    $big 999999999;
          
    $total = isset($main_query->max_num_pages)?$main_query->max_num_pages:'';
          if(
    $total 1) echo '<div class="paginate_links">';
          echo 
    paginate_links( array(
             
    'base'        => str_replace$big'%#%'esc_urlget_pagenum_link$big ) ) ),
             
    'format'   => '?paged=%#%',
             
    'current'  => max1get_query_var('paged') ),
             
    'total'    => $total,
             
    'mid_size' => '10',
             
    'prev_text'    => __('<','itseovn'),
             
    'next_text'    => __('>','itseovn'),
          ) );
          if(
    $total 1) echo '</div>';
    }
    Chèn code sau vào vị trí cần hiển thị:
    PHP:
       <?php wp_corenavi_table();?>
    Sửa lại theo Code sẽ phân trang đổi lại như sau:
    PHP:
    <div class="large-9 col">
        <?php
            $paged 
    = (get_query_var('paged')) ? get_query_var('paged') : 1;
            
    $args =  array(
                
    'post_type' => 'hoidap',
                
    'paged' => $paged,
                
    'order' => 'DECS',
                
    'posts_per_page' => 5,
                
    'orderby'=>'date'
            
    );
            
    $loop = new WP_Query($args);
            
    $i 1;
            if(
    $loop->have_posts()) {
                while (
    $loop->have_posts()) {
                    
    $loop->the_post();
                    
    ?>
                    <div class="csq-item">
                        <div class="csq-title-item">
                            <span class="csq-number"><?php echo str_pad($i2'0'STR_PAD_LEFT); ?></span>
                            <p class="csq-title">Khách hỏi: <?php echo get_field('question'?></p>
                        </div>
                        <div class="csq-question">
                            <span>Câu hỏi:</span>
                            <h3><?php echo get_field('question'?></h3>
                        </div>
                        <div class="csq-anwser">
                            <label>Trả lời:</label>
                            <p>Chuyên gia trả lời: <?php echo get_field('name_author'?></p>
                            <div class="csq-content">
                                <?php the_content() ?>
                            </div>
                        </div>
                    </div>
                    <?php
                    $i
    ++;
                }
            }
        
    ?>
        <?php if (  $loop->max_num_pages ):
            
    ?>
            <nav class="navigation">
                <?php
                    $main_query 
    $loop;
                    
    $big 999999999;
                    
    $total = isset($main_query->max_num_pages)?$main_query->max_num_pages:'';
                    if(
    $total 1) echo '<div class="paginate_links">';
                    echo 
    paginate_links( array(
                        
    'base'        => str_replace$big'%#%'esc_urlget_pagenum_link$big ) ) ),
                        
    'format'   => '?paged=%#%',
                        
    'current'  => max1get_query_var('paged') ),
                        
    'total'    => $total,
                        
    'mid_size' => '10',
                        
    'prev_text'    => __('<','itseovn'),
                        
    'next_text'    => __('>','itseovn'),
                    ) );
                    if(
    $total 1) echo '</div>';
                
    ?>
            </nav>
        <?php endif; ?>
    </div>
    Chèn thêm CSS vào cho đẹp

    HTML:
    /*paginate_links*/
    .paginate_links {
        overflow: hidden;
        text-align: center;
        display: table;
        margin: 30px auto;
    }
    .paginate_links .page-numbers{
        width: 32px;
        height: 32px;
        float: left;
        margin: 0 5px 0px 5px;
        padding-top: 3px;
        color: #3a589d;
        border: 1px solid #3a589d;
    }
    .paginate_links .page-numbers:hover{
        background: #3a589d;
        color: #fff;
    }
    .paginate_links .page-numbers.prev,.paginate_links .page-numbers.next{
        background: transparent;
        width: auto;
        border: none;
    }
    .paginate_links .page-numbers.prev:hover,.paginate_links .page-numbers.next:hover{
        text-decoration: underline;
        color: #3a589d;
    }
    .paginate_links .page-numbers.current{
        background: #3a589d;
        color: #fff;
    }
    /*End paginate_links*/
     
    Chỉnh sửa cuối: 30/10/22


Chủ để tương tự : code phân
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Code download pdf wordpress in Function.php như thế nào? 9/12/23
Mã nguồn mở Wordpress Code download file .pdf in function.php wordpress? 30/10/23
Mã nguồn mở Wordpress Xin code tự động tạo user trong function.php wordpress 6/10/23
Mã nguồn mở Wordpress Lấy thứ, ngày, tháng, năm, giờ:phút:giây TizeZone trong code PHP wordpress 2/7/22
Mã nguồn mở Wordpress ShortCode danh sách sản phẩm nổi Featured trong WooCommerce 11/10/21