使用WP_QUERY无法进行分页

时间:2019-11-04 作者:Shadow

我已经为自定义post类型分页编写了代码,但它不起作用。下面是一些代码。

<?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>

1 个回复
SO网友:divyaT

为自定义帖子设置页面导航的最简单方法是使用WP PageNavi. 激活此插件后,您需要在自定义帖子模板中添加以下代码。

<?php
    if(function_exists(\'wp_pagenavi\')) {
        wp_pagenavi( array(
            \'query\' =>$loop
        ));
    }

相关推荐

get_post_meta pagination

我有自定义字段,具有多个值的数组$metas = get_post_meta($post->ID, \"$meta_key\", false); foreach ( $metas as $metas ){ echo $metas; } 当然,这将返回一个数组。如何对此分页?例如:www.example.com/post-title/1 将返回meta_value[0]和www.example.com/post-title/2 将