简体   繁体   English

WordPress分页-不起作用-自定义插件

[英]WordPress Pagination - Not Working - Custom Plugin

So I'm currently working on a custom plugin that is relying on custom post types. 因此,我目前正在开发依赖于自定义帖子类型的自定义插件。 I am then displaying these custom post types in an admin page but can't seem to get the pagination to display! 然后,我在管理页面中显示这些自定义帖子类型,但似乎无法显示分页! I have no idea what I'm doing wrong. 我不知道我在做什么错。

This is the loop I am running: 这是我正在运行的循环:

<?php
        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

        $mypost = array( 'posts_per_page' =>'1','paged'=>$paged,'post_type' => 'usernotes', 'meta_query' => array(
                array(
                    'key' => 'userNotesID',
                    'value' => get_current_user_id( )
                )
    ));
        $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
    <section class="innerWrapper">
            <article id="<?php the_ID(); ?>" class="note" <?php post_class(); ?>>
            <header class="entry-header">
                        <!-- Display Title and Author Name -->
                        <span class="title"><?php the_title(); ?></span>
                        <span class="date"><?php the_time(get_option('date_format')); ?></span>  
                </header>

                <div class="noteInner">
                    <?php the_content(); ?>
                </div>

                <footer>
                    <a href="'#" class="delete button-red" id="<?php the_ID(); ?>">Hide Note</a>
                    <div class="clear"></div>
                </footer>
            </article>
</section>


    <?php endwhile; ?>

It displays 1 post as it should and if I increase the posts_per_page to 2 then it as it should shows 2 posts. 它按应显示1个帖子,如果我将posts_per_page增加到2,则应显示2个帖子。 The problem is I can't get any pagination links to work. 问题是我无法获得任何分页链接。 If I have it set to 1 and then navigation to ?paged=2, nothing changes, it just shows the first post only! 如果我将其设置为1,然后导航到?paged = 2,则没有任何变化,它仅显示第一篇文章! Any idea why or what I can try? 知道为什么或可以尝试什么吗?

EDIT: 编辑:

Should probably add this is on an admin page, displaying custom post types. 可能应该在管理页面上添加此内容,以显示自定义帖子类型。

What about using a 'current' attribute in your query? 在查询中使用“当前”属性该怎么办? Such as

'current' => max( 1, get_query_var('paged') )

I forgot where did I find this function but it works without any problem in my several projects. 我忘记了在哪里找到此功能,但在我的几个项目中都可以正常使用。 It makes things a lot easier to read and edit too. 它也使事情变得更容易阅读和编辑。

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination($wp_query) {

        $big = 999999999; // need an unlikely integer

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'prev_text'    => __('<'),
            'next_text'    => __('>'),
            'type'         => 'plain',
        ) );
    }
endif;    

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM