简体   繁体   中英

Wordpress Pagination - can't access to second page

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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