简体   繁体   English

WordPress分页在每个页面上显示相同的帖子

[英]WordPress Pagination showing same posts on every page

I have been building a website recently and I faced a problem with the pagination. 我最近一直在建立网站,但分页时遇到了问题。 I am getting same posts (portfolio post in this case) on each page. 我在每个页面上收到相同的帖子(在这种情况下为投资组合帖子)。 I've been googling around how to solve this but it seems that this problem is individual on each theme. 我一直在寻找如何解决这个问题的方法,但似乎这个问题在每个主题上都是个别的。

This is the code from the page template I'm using... 这是我正在使用的页面模板中的代码...

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); 

    $pag = get_post_meta( $post->ID, 'folio_pag', true );
    $per = get_post_meta( $post->ID, 'folio_per', true );

    $style = get_post_meta( $post->ID, 'folio_style', true );
    $animation = get_post_meta( $post->ID, 'folio_anim', true );
    $color = get_post_meta( $post->ID, 'folio_color', true );
    $info = get_post_meta( $post->ID, 'folio_info', true );

    $ratio = get_post_meta( $post->ID, 'folio_ratio', true );
    $r = explode( ':', $ratio );

    $cols = get_post_meta( $post->ID, 'folio_cols', true );
    $c = $cols == 'two' ? 648 : ( $cols == 'three' ? 432 : 324 );
    $img_factor = '0';

    $cats = get_post_meta( $post->ID, 'folio_cats', true );
    $query_filter = '';
    $filter_output = '<li><a href="#" data-filter="*" class="selected">' . __( 'All', 'krown' ) . '</a></li>';
    $show_cats = get_post_meta( $post->ID, 'folio_filter', true ) == 'enable-filters' ? true : false;

    if ( ! empty ( $cats ) ) {

        if ( sizeof( $cats ) == 1 ) {
            $show_cats = false;
        }

        foreach ( $cats as $cat ) {

            $filter = get_term_by( 'id', $cat, 'portfolio_category' ); 

            $query_filter .= $filter->slug . ', ';

            $filter_output .= '<li><a href="#" data-filter=".' . $filter->slug . '">' . $filter->name . '</a></li>';

        }

    } else {

        $cats = get_categories( array( 'taxonomy'=>'portfolio_category' ) );

        foreach ( $cats as $cat ) {
            $filter_output .= '<li><a href="#" data-filter=".' . $cat->slug . '">' . $cat->name . '</a></li>';
        }

    }

?>

<?php if ( get_post_meta( $post->ID, 'folio_content', true ) == 'content-above' ) {
    the_content(); 
} ?>

<?php if ( $show_cats ) {

    echo '<div id="filter"><ul class="clearfix">' . $filter_output . '</ul></div>';

} ?>

<div id="portfolio-holder" class="clearfix">

    <ul id="portfolio" class="folio-grid cols-<?php echo $cols; ?> style-<?php echo $animation . ' ' . $color; ?> show-<?php echo $info; ?> layout-<?php echo $style; ?> clearfix">

        <?php

        $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : ( get_query_var( 'page' ) ? get_query_var( 'page' ) : 1 );

        $args = array(
            'post_type' => 'portfolio',
            'portfolio_category' => $query_filter,
            'offset' => 0,
            'posts_per_page' => ( $pag == 'no-pagination' ? -1 : $per ),
            'paged' => $paged
        );

        $all_posts = new WP_Query( $args ); 

        $page_id = $post->ID;

        while ( $all_posts->have_posts() ) : $all_posts->the_post();

            $thumb = get_post_thumbnail_id();
            $img_url = wp_get_attachment_image_src( $thumb, 'full' );

            if ( $thumb == '' ) {
                $img_url = Array( get_template_directory_uri() . '/images/blank_2.gif' );
            }

            $retina = krown_retina();
            $retina_thumb = get_post_meta( $post->ID, 'portfolio_retina-thumbnail_thumbnail_id', true );

            if ( $style == 'fixed' ) {

                switch ( $cols ) {

                    case 'two':
                        $img_width = 648;
                        $img_height = ceil( $img_width / $r[0] * $r[1] );
                        break;

                    case 'three':
                        $img_width = 432;
                        $img_height = ceil( $img_width / $r[0] * $r[1] );
                        break;

                    default:
                        $img_width = 324;
                        $img_height = ceil( $img_width / $r[0] * $r[1] );
                        break;

                }

                if ( $retina === 'true' && $retina_thumb != '' ) {

                    $retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
                    $image = aq_resize( $retina_url[0], $img_width*2, $img_height*2, true, false );

                } else {

                    $image = aq_resize( $img_url[0], $img_width, $img_height, true, false ); 

                }

            } else if ( $style == 'masonry' ) {

                $img_factor = 1;
                $img_width = $c;

                if ( $retina === 'true' && $retina_thumb != '' ) {

                    $retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
                    $image = aq_resize( $retina_url[0], $img_width*2, null, false, false );

                } else {

                    $image = aq_resize( $img_url[0], $img_width, null, false, false );

                }  

            } else if ( $style == 'masonry-advanced' ) {

                $img_factor = floor( $img_url[1] / $c );

                $img_width = $img_factor * $c;

                if ( $retina === 'true' && $retina_thumb != '' ) {

                    $retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
                    $image = aq_resize( $retina_url[0], $img_width*2, null, false, false );

                } else {

                    $image = aq_resize( $img_url[0], $img_width, null, false, false );

                }  

            }


        ?>

            <li class="item <?php krown_categories( $post->ID, 'portfolio_category', ' ', 'slug' ); ?>" data-factor="<?php echo $img_factor; ?>">

                <a href="<?php echo get_new_permalink( $page_id, $post->ID, $cats ); ?>">

                    <img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php the_title(); ?>" />

                    <div class="caption">

                        <div>

                            <div>

                                <h3><?php the_title(); ?></h3>

                                <span class="category"><?php krown_categories( $post->ID, 'portfolio_category' ); ?></span>
                                <span class="excerpt"><?php echo krown_excerpt( 'krown_excerptlength_post' ); ?></span>

                            </div>

                        </div>

                    </div>

                </a>

            </li>

        <?php endwhile; 

        ?>

    </ul>

</div>

<?php if ( $pag == 'classic' ) {
    krown_pagination( $all_posts, true );
} else if ( $pag == 'infinte-loading' ) {
    echo '<div class="infinite-barrier"><span class="preloader"></span><p class="end">' . __( 'No More Projects', 'krown' ) . '</p><a id="infinite-link" href="' . next_posts( 0, false ) . '">' . __( 'Load More Projects', 'krown' ) . '</a></div>';
} ?>

<?php wp_reset_query(); ?>

<?php if ( get_post_meta( $post->ID, 'folio_content', true ) == 'content-below' ) {
    the_content(); 
} ?>    

I think the reason why the same content is showing in every other pages because you set offset to 0 in the portfolio query. 我认为每隔一页显示相同内容的原因是因为在投资组合查询中将offset设置为0。 You should update your offset $offset = $per * ($paged-1) so that every page change would tell WP_Query to offset right amount of posts that match current page context. 您应该更新偏移量$offset = $per * ($paged-1) WP_Query $offset = $per * ($paged-1)以便每次页面更改都会告诉WP_Query偏移与当前页面上下文匹配的正确数量的帖子。

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

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