duanfu6160 2016-10-18 18:36
浏览 86
已采纳

Wordpress多个帖子类型循环在每个页面上显示相同的结果

So I am using WordPress with an underscores theme. I used code from twenty fourteen's theme with pagination.

if ( ! function_exists( 'public_notices_paging_nav' ) ) :
function public_notices_paging_nav() {

global $wp_query, $wp_rewrite;

// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
    echo ("<h4 class='one-page-results'>End of Results</h4>");
    return;
}

$paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args   = array();
$url_parts    = explode( '?', $pagenum_link );

if ( isset( $url_parts[1] ) ) {
    wp_parse_str( $url_parts[1], $query_args );
}

$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

$format  = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';

// Set up paginated links.
$links = paginate_links( array(
    'base'     => $pagenum_link,
    'format'   => $format,
    'total'    => $wp_query->max_num_pages,
    'current'  => $paged,
    'mid_size' => 1,
    'add_args' => array_map( 'urlencode', $query_args ),
    'prev_text' => __( '&larr; Previous', 'public_notices' ),
    'next_text' => __( 'Next &rarr;', 'public_notices' ),
    'type'      => 'list',
) );

if ( $links ) :

?>

<nav class="navigation paging-navigation" role="navigation">
    <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'public_notices' ); ?></h1>
    <?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}endif;

This worked great when I only had one post type, but now I've made a loop that displays two post types and it works. But on every page its the same group of posts.

<?php
      $postLoop = array(
        'post_type' => array('public_notice_post', 'post'),
            'orderby' => 'date',
            'order' => 'DESC'
    );
    $query = new WP_Query( $postLoop );
                 if ($query->have_posts()) :
                     while ($query->have_posts()) : $query->the_post(); 
                     get_template_part( 'template-parts/content', get_post_format() );
                endwhile; 

                public_notices_paging_nav();

                endif;

     wp_reset_query(); ?>  

Anything will help. I've tried a lot.

  • 写回答

2条回答 默认 最新

  • dpowt82802 2016-10-19 17:15
    关注

    To fix the original problem of duplicated posts on every page all I had to do was write my code like this. HOWEVER I am still having an issue of page count, but that will addressed elsewhere.

            <?php  
            $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    
            $ppp = get_option( 'posts_per_page' );      
    
            if ( $page == 1 )
                $offset = 6;
            else
                $offset = 6 + ( $page - 1 ) * $ppp;
        ?>
        <?php
          $postLoop = array(
                'post_type' => array('public_notice_post', 'post'),
                'orderby' => 'date',
                'order' => 'DESC',
                'paged' => $paged,
                'posts_per_page'=> $ppp,
                'offset' => $offset
        );
    
                $wp_query = new WP_Query( $postLoop );
                     if ($wp_query->have_posts()) :
                         while ($wp_query->have_posts()) : the_post(); 
                         get_template_part( 'template-parts/content', get_post_format() );
                    endwhile; 
    
                    public_notices_paging_nav();
    
                    endif;
    
    
         wp_reset_query(); ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 stm32用5个IO口接16个矩阵按键怎么设计PCB原理图啊