-1

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>
-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
        ));
    }
| improve this answer | |
  • No, I don't want to go with a plugin, I believe in custom code here – Shadow Nov 4 '19 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.