WordPress /page/2 not working

时间:2014-01-16 作者:Storm3y

单击next\\u posts\\u链接时,只会显示与第1页相同的帖子。它似乎在分类页面上运行良好,只会影响主页(home.php)

谢谢

    <?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; ?>
<?php query_posts(\'cat=-683,-3598\' . $paged); ?>
完整代码

    <?php get_header(); ?>
    <div id="featured">
            <div id="featured_left">
                <div id="block1">
                <?php query_posts(\'cat=3598&showposts=1\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox12\'); ?>

<?php endwhile; ?>

                </div>
                <div id="block2">
                <?php query_posts(\'cat=3598&showposts=1&offset=1\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox12\'); ?>

<?php endwhile; ?>

                </div>
            </div>
            <div id="featured_middle">
                <div id="block3">
                <?php query_posts(\'cat=3598&showposts=1&offset=2\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox3\'); ?>

<?php endwhile; ?>

                </div>
                <div id="block6">
                <?php query_posts(\'cat=3598&showposts=1&offset=3\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox67\'); ?>

<?php endwhile; ?>

                </div>
                <div id="block7">
                <?php query_posts(\'cat=3598&showposts=1&offset=4\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox67\'); ?>

<?php endwhile; ?>

                </div>
            </div>
            <div id="featured_right">
                <div id="block4">
                <?php query_posts(\'cat=3598&showposts=1&offset=5\'); ?>
<?php while(have_posts()) : the_post(); ?>

<?php the_post_thumbnail(\'fbox45\'); ?>

<?php endwhile; ?>

                </div>



            </div>
        </div>


        <div id="left">
        <?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; ?>
   <?php query_posts(\'cat=-683,-3598&paged=\' . $paged); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <div class="post_item">
        <?php
if ( has_post_thumbnail() ) {
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'homepage-thumb\'); ?></a>
<?php }
else { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/no_thumb.png"/></a>
<?php }
?>
        <h2><?php the_category(\', \'); ?> | <?php the_time(\'F j, Y\'); ?></h2>
        <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_excerpt(); ?></a>
                </div>
                <?php endwhile; ?>
                    <?php endif; ?>
        <div class="pagenav">
<div class="pagenavright"><?php next_posts_link(\'Next Entries\',\'\') ?></div>
</div> <!-- end navigation -->          
        </div>

    <?php get_sidebar(); ?>

    </div>

<?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:Milo 整理而成

Don\'t use query_posts, ever.

前6个查询可以压缩为一个WP_Query:

$args = array(
    \'cat\' => 3598,
    \'posts_per_page\' => 6
);

$featured = new WP_Query( $args );

if( $featured->have_posts() ){

    $featured->the_post();
    ?>
    your markup for the first post
    <?php

    $featured->the_post();
    ?>
    your markup for the second post
    <?php

    $featured->the_post();
    // etc..

    wp_reset_postdata();
}
对于主查询,使用pre_get_posts 在主题的functions.php. 这样可以避免在模板中覆盖查询时浪费查询。

function wpa_exclude_categories( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( \'category__not_in\', array( 683, 3598 ) );
    }
}
add_action( \'pre_get_posts\', \'wpa_exclude_categories\' );
现在,您已经从每个页面加载中删除了6个查询,您的分页将神奇地工作,只需跳一小段舞。

SO网友:Abdul Awal Uzzal

<?php query_posts(\'cat=-683,-3598\' . $paged); ?>
该行应为

<?php query_posts(\'cat=-683,-3598&paged=\' . $paged); ?>

结束