Wordpress分页不适用于wp_pagenavi

时间:2014-01-21 10:08:39

标签: wordpress wordpress-theming

我正在使用短代码显示帖子,它只显示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就失败了,它会显示所有帖子。

请帮我分页,非常感谢你。

1 个答案:

答案 0 :(得分:0)

我认为您使用插件来显示分页,最好是创建您的自定义分页。 在 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(); ?>