1

我有以下代码:

$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;');
}

可以在这里看到实际操作。此页面当前显示数据库中 21 个帖子中的 3 个。怎么没有分页。

有人能告诉我为什么吗?

4

2 回答 2

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>
于 2014-12-25T17:00:25.533 回答
0

两者next_posts_linkprevious_posts_link使用全局$wp_query$paged您必须在源代码周围跟踪函数调用才能看到这一点(但使用 非常明显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

于 2013-01-11T23:53:04.633 回答