分页元素不在WordPress循环中显示

时间:2013-01-11 20:45:27

标签: php wordpress pagination wordpress-theming

我有以下代码:

$attr = array(
        'align' => 'left',
        'class' => 'thumbnail imageRight',
        'width' => 350,
        'height' => 350
);

$post_query =  array ( 'post_type' => 'post' );
$posts = new WP_Query ( $post_query );

if($posts->have_posts()){
    while($posts->have_posts()){
        $posts->the_post();
        ?>
        <div class="post">
            <?php the_post_thumbnail('medium', $attr); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <p><?php the_excerpt(); ?></p>
        </div>
        <?php
    }

    next_posts_link('&laquo; Older Entries');
    previous_posts_link('Newer Entries &raquo;');
}

可以在行动here中看到。此页面显示数据库中21个帖子中的3个。怎么没有分页。

有人可以告诉我为什么吗?

2 个答案:

答案 0 :(得分:0)

next_posts_linkprevious_posts_link都使用全局$wp_query$paged。你必须在the source code周围追逐函数调用才能看到(但next_post_links显然很明显)。它们不适用于自定义查询,但我相信你可以作弊。

$old_wpq = $wp_query;
$wp_query = new WP_Query ( $post_query );
// your loop
$wp_query = $old_wpq;

试试。

有一个相关的主题wordpress.stackexchange.com。

https://wordpress.stackexchange.com/questions/77661/next-posts-link-works-only-with-original-wp-query/77666#77666

答案 1 :(得分:0)

<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(  
            'post_type' => 'post', //Post type
            'posts_per_page' => 3, //How many post u want to display per page
            'paged' => $paged                      
            );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
    <img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <p><?php the_excerpt(); ?></p>

<?php } } ?>
<div class="pagination">
<?php             
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
?>
</div>