douchixu3686 2017-06-07 06:00
浏览 114

Wordpress分页 - 无法访问第二页

I use WP Beginner pagination (without plugin) on my website, but I can't access to second and other pages.

<?php

$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;

 $custom_query_args = array(
        'post_type' => array( 'tutorials','post' ),
        'posts_per_page' => 2,
        'cat' => $cat_id,
 );

// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

// Output custom query loop
if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts() ) :
                        $custom_query->the_post();

            echo '<article class="other-post col-xs-12 col-sm-12 col-md-3 col-lg-3">';
            echo '<div class="back-color">';
            echo '<h3>';
            echo the_post_thumbnail('post-thumbnail', array( 'class' => 'post-image' ));
            echo '<a href="'. get_permalink() .'" title="'. get_the_title() .'">';
            echo '<span><b>'. get_the_title() .'</b></span>';
            echo '</a>';
            echo '</h3>';
            echo '</div>';
            echo '</article>';

        endwhile;
endif;

// Reset postdata
wp_reset_postdata();

// Custom query loop pagination
wpbeginner_numeric_posts_nav();


// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>

Here is code from function.php, but I think it isn't the problem. http://virtual-wizard.eu/function.txt

展开全部

  • 写回答

2条回答 默认 最新

  • doudi5524 2017-06-07 06:24
    关注

    You’re missing the paged parameter in your query args.

    I would also replace the page query, so instead of this:

    // Get current page and append to custom query parameters array
    $custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    

    You have this:

    // Get current page and append to custom query parameters array
    if ( get_query_var( 'paged' ) ) {
        $paged = get_query_var( 'paged' );
    } elseif ( get_query_var( 'page' ) ) {
        $paged = get_query_var( 'page' );
    } else {
        $paged = 1;
    }
    

    And then change your query to this:

    $custom_query_args = array(
       'post_type' => array( 'tutorials','post' ),
       'posts_per_page' => 2,
       'cat' => $cat_id,
       'paged' => $paged,
    );
    

    I hope that helps.

    评论
  • duanchi3109 2017-06-08 10:45
    关注

    Try using this code instead. Make sure to read through as they are explaining how to implement this function into your WP website.

    http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

    Function is setup inside of a functions.php and on the specific page when you want to display pagination just call the function like pagination();

    Before calling out pagination check also the CSS part and structure of the pagination to get the desired effect.

    Srecno!

    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部