简体   繁体   English

分页不适用于wordpress自定义帖子

[英]Pagination Doesn't work with wordpress custom post

Pagination Doesn't work with word press custom post. 分页不适用于Word press自定义帖子。 i have tried these code below,but pagination doesn't appear. 我已经在下面尝试过这些代码,但是没有出现分页。 tried different query but no result. 尝试了不同的查询,但没有结果。 i am new in php and word press.i just copy and past code. 我是php和word press.i的新手,我只是复制和过去的代码。 can anyone please help me? 谁能帮帮我吗? what i have done so far are below. 我到目前为止所做的事情如下。

in function.php 在function.php中

/*pagination*/  

function wpbeginner_numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class="navigation"><ul>' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

in custom post query 在自定义帖子查询中

<?php
                            global $post;
                            $args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
                            $myposts = get_posts( $args );
                            foreach( $myposts as $post ) : setup_postdata($post); 
                        ?>
                        <div class="page_news">
                        <div class="single_page_news">
                            <h2><?php the_title(); ?><h2>
                            <p><?php the_content(); ?></p>
                        </div>
                        </div>
                        <?php endforeach; ?>

                        <?php wpbeginner_numeric_posts_nav(); ?>

please help me 请帮我

Use These code 使用这些代码

$paged = ( get_query_var('page') ) ? get_query_var('page') :1;
$query = new WP_Query( array( 'posts_per_page' => 1,'paged' => $paged,'post_type' => 'achievements','orderby' => 'date', 'order' => 'ASC' ) );
while ( $query->have_posts() ) : $query->the_post();

endwhile; 最后

Instead of 代替

$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
 $myposts = get_posts( $args );

Follow this link http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/ 请点击此链接http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/

Your pagination function is only set up for the default main query, not for a custom query. 分页功能仅针对默认的主查询设置,而不针对自定义查询设置。

Also, don't use get_posts for paginated queries. 另外,请勿对分页查询使用get_posts It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination. 这是用于自定义查询的很好的功能,但是一旦需要分页,它就会变得无法使用。

Rather use WP_Query for paginated queries, it is much easier to use. WP_QueryWP_Query用于分页查询, WP_Query使用它容易得多。

Example: 例:

$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) :  1;

$args = array(
   'posts_per_page' => 1,
   'paged' => $paged,
   'post_type' => 'YOUR POST TYPE'
);

$q = new WP_Query($args);

if($q->have_post()) {
   while($q->have_posts()) {
     $q->the_post();

     //YOUR LOOP

   }
  //YOUR PAGINATION
}
wp_reset_postdata();

You can have a look at the codex for extra parameters. 您可以查看编解码器中的其他参数。

You now need to change every instance of $wp_query in your pagination function to $q for it to work. 现在,您需要将分页功能中$wp_query每个实例更改为$q才能起作用。

Just a point of note, you don't have to call the $post global 只是需要注意的一点,您不必将$post称为全局

EDIT 编辑

From your comments, there is a much easier way to accomplish your goal without any custom query 从您的评论中,您可以轻松实现目标,而无需任何自定义查询

This page is an archive page that is meant to display your custom post type latestnews . 此页面是一个存档页面,旨在显示您的自定义帖子类型latestnews You can simply just rename your archive-custom.php to archive-latestnews.php . 您只需将您的archive-custom.php重命名为archive-latestnews.php See the Template Hierarchy . 请参阅模板层次结构 Just make sure has_archive is set to true when you register your post type 只需在注册帖子类型时确保has_archive设置为true

You should also never shop the main query for a custom query on any type of archive page. 您也永远不要在任何类型的存档页面上为主要查询购买主查询。 It is always troublesome, as you can see. 如您所见,它总是很麻烦。 So, delete your custom query and replace it with the default query 因此,删除您的自定义查询并将其替换为默认查询

This is all you should have in your archive page 这是您存档页面中应该拥有的所有内容

if(have_post()) {
   while(have_posts()) {
     the_post();

     //YOUR LOOP

   }
  //YOUR PAGINATION
}

Just change all the instances of $q back to $wp_query again. 只需将$q所有实例再次更改回$wp_query Everything should work then 一切都应该正常工作

For extra info, you have to check out this post I've done on WPSE 对于额外的信息,你必须看看这个帖子 ,我就做WPSE

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

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