【问题标题】:Wordpress pagination not working with wp_pagenaviWordpress 分页不适用于 wp_pagenavi
【发布时间】:2014-01-21 18:30:56
【问题描述】:

我正在使用简码显示帖子,它只显示 5 个帖子,我正在使用 wp_pagenavi 插件进行分页,但没有显示分页。以下是我的代码,请帮忙。

global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'cat' => '2', 'paged' => $paged );
$loop = new WP_Query( $args );  
if ( $loop->have_posts() ) {
    ob_start();
?>
<div class="clearfix"></div>
<div class="issue-articles-wrap all-articles-wrap">
    <?php
    while ( $loop->have_posts() ) : $loop->the_post();
        $loop->the_post();
        $thumb_url = '';
        $full_width = 'full-width-article';
        if( has_post_thumbnail() ){
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
            $thumb_url = $thumb['0'];
            $full_width = '';
        }
        if( $post->ID > 0 ){
        ?>
        <div class="issue-item issue-item-<?php echo $post->ID; ?>">
            <?php if( $thumb_url != '' ){ ?>
            <div class="thumb-article">
                <a href="<?php the_permalink(); ?>"><img src="<?php echo $thumb_url; ?>" alt="<?php the_title(); ?>"></a>
            </div><!--thumb-article-->
            <?php } ?>
            <div class="summary-article <?php echo $full_width; ?>">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php the_excerpt(); ?>
            </div><!--summary-article-->
            <div class="clearfix"></div>
        </div><!--issue-item-->
        <?php
        }   //  end of if( $post->ID > 0 )
    endwhile;
    wp_pagenavi(); 
    wp_reset_query();
    ?>
</div><!--issue-articles-wrap all-articles-wrap-->
<?php   
$rep = ob_get_contents();
ob_end_clean();
}   //  end of if ( $loop->have_posts() )


return $rep;

我也试过自定义分页,但失败了,如果我通过posts_per_page = -1,它会显示所有帖子。

请帮我运行分页,非常感谢您。

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    我认为您正在使用插件来显示分页,最好创建您的自定义分页。 在你的 function.php 中写下这段代码:

    if ( ! function_exists( 'your_paging_nav' ) ) :
    
    function your_paging_nav() {
        global $wp_query;
    
        // Don't print empty markup if there's only one page.
        if ( $wp_query->max_num_pages < 2 )
            return;
        ?>
        <nav>
            <h1><?php _e( 'Posts navigation', 'framwork-translation' ); ?></h1>
            <div>
    
                <?php if ( get_next_posts_link() ) : ?>
                <div class="nav-previous"><?php next_posts_link( __( 'Older posts', 'framwork-translation' ) ); ?></div>
                <?php endif; ?>
    
                <?php if ( get_previous_posts_link() ) : ?>
                <div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'framwork-translation' ) ); ?></div>
                <?php endif; ?>
    
            </div>
        </nav>
        <?php
    }
    endif;
    

    在 index.php 中

    <body <?php body_class(); ?>>
    <?php get_header(); ?>
    
    <div class="primary">
      <?php if ( have_posts() ) : ?>
    
        <!-- The loop -->
    
        <?php while ( have_posts() ) : the_post(); ?>
          <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>
    
        <?php your_paging_nav(); ?>
    
      <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    
      <?php endif; ?>
    </div>
    
    <?php get_footer(); ?>
    

    【讨论】:

    • 非常感谢您的友好回复,我将 WP_Query 更改为 query_posts 并且它工作正常,我还有另一个分页问题,​​即使用 .html 扩展名,您能看看问题stackoverflow.com/questions/21255909/… 请?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多