最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Pagination is not working using WP_Query

programmeradmin3浏览0评论

I have coded for custom post type pagination but it doesn't work. Here is some code for it.

<?php
global $wpdb;
$userid = get_current_user_id();
$userData = get_user_meta($userid , 'referat', true);

$args = array(
    'post_type' => 'zt_lawyersportal',
    'posts_per_page' => 2,
    'orderby'     => 'modified',
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
    'meta_query' => array(
        array(
            'key' => 'referat',
            'value' => $userData,
            'compare' => 'IN',
        ),
    ),
);
$resData = new WP_Query($args); ?>
<div class="post">
    <h2>Posteingang</h2>
    <div class="table-responsive">  
        <table class="table">
            <thead>
                <tr>                                    
                    <th>postname</th>
                    <th>Aktionen</th>
                </tr>
            </thead>
            <tbody id="dahboard-tbody">
                <?php
                if ($resData->have_posts()) :
                    while ($resData->have_posts()) :
                        $resData->the_post();
                        ?>
                        <tr onclick="location.href = '<?= get_permalink(); ?>';"  data-rowid="<?= $post->ID; ?>">                                           
                            <td><?= get_the_title(); ?></td>
                            <td class="last">
                                <a class="green" href="<?= get_permalink($post->ID) ?>">Fall anzeigen</a>
                            </td>
                        </tr>
                        <?php
                    endwhile;
                else:
                    echo '<tr><td><h2>Keine Fälle gefunden<h2></td></tr>';
                endif;
                wp_reset_postdata();
                ?>
            </tbody>
        </table>
        <div class="col-sm-12">
            <?php
            $total_pages = $resData->max_num_pages;
            if ($total_pages > 1) {
                $current_page = max(1, get_query_var('paged'));
                $page =  paginate_links(array(
                    'base' => get_pagenum_link(1) . '%_%',
                    'format' => '/page/%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'prev_text' => __('Prev'),
                    'next_text' => __('Next'),
                    'type' => 'array'
                ));
                echo '<nav aria-label="Page navigation example"><ul class="pagination">';
                foreach ($page as $key => $value) {
                    echo '<li class="page-item">' . $value . '</li>';
                }   
                echo '</ul></nav>';
            }
            ?>
        </div>
    </div>

I have coded for custom post type pagination but it doesn't work. Here is some code for it.

<?php
global $wpdb;
$userid = get_current_user_id();
$userData = get_user_meta($userid , 'referat', true);

$args = array(
    'post_type' => 'zt_lawyersportal',
    'posts_per_page' => 2,
    'orderby'     => 'modified',
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
    'meta_query' => array(
        array(
            'key' => 'referat',
            'value' => $userData,
            'compare' => 'IN',
        ),
    ),
);
$resData = new WP_Query($args); ?>
<div class="post">
    <h2>Posteingang</h2>
    <div class="table-responsive">  
        <table class="table">
            <thead>
                <tr>                                    
                    <th>postname</th>
                    <th>Aktionen</th>
                </tr>
            </thead>
            <tbody id="dahboard-tbody">
                <?php
                if ($resData->have_posts()) :
                    while ($resData->have_posts()) :
                        $resData->the_post();
                        ?>
                        <tr onclick="location.href = '<?= get_permalink(); ?>';"  data-rowid="<?= $post->ID; ?>">                                           
                            <td><?= get_the_title(); ?></td>
                            <td class="last">
                                <a class="green" href="<?= get_permalink($post->ID) ?>">Fall anzeigen</a>
                            </td>
                        </tr>
                        <?php
                    endwhile;
                else:
                    echo '<tr><td><h2>Keine Fälle gefunden<h2></td></tr>';
                endif;
                wp_reset_postdata();
                ?>
            </tbody>
        </table>
        <div class="col-sm-12">
            <?php
            $total_pages = $resData->max_num_pages;
            if ($total_pages > 1) {
                $current_page = max(1, get_query_var('paged'));
                $page =  paginate_links(array(
                    'base' => get_pagenum_link(1) . '%_%',
                    'format' => '/page/%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'prev_text' => __('Prev'),
                    'next_text' => __('Next'),
                    'type' => 'array'
                ));
                echo '<nav aria-label="Page navigation example"><ul class="pagination">';
                foreach ($page as $key => $value) {
                    echo '<li class="page-item">' . $value . '</li>';
                }   
                echo '</ul></nav>';
            }
            ?>
        </div>
    </div>
Share Improve this question edited Nov 4, 2019 at 8:44 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Nov 4, 2019 at 6:31 ShadowShadow 685 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -2

The easiest way to have page nav for the custom post is to use WP PageNavi. After activating this plugin you need to add the following code in your custom post template.

<?php
    if(function_exists('wp_pagenavi')) {
        wp_pagenavi( array(
            'query' =>$loop
        ));
    }
发布评论

评论列表(0)

  1. 暂无评论