1

在我的管理员中,我有 12 个贴有“努力”标签的帖子,尽管还有很多其他帖子。

标记为“努力”的 3 个最新帖子显示在主页上,其余的则显示在一个页面中,标题为“努力”也就不足为奇了

在那个努力页面上,我需要每页有 5 个帖子的分页,但也有 3 个偏移量。

在似乎无法再找到的博客文章中,建议如下:

$temp = $wp_query;

$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);

wp_reset_query();
wp_reset_postdata();

$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);

这很好用,只是分页页太多,最后一页是空白的。

为什么'Previous Entries'链接出现在第2页并带您进入空白页面。

这是完整的代码供参考:

<?php
global $options;
foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { 
        $$value['id'] = stripslashes( $value['std'] ); 
    } else { 
        $$value['id'] = stripslashes( get_settings( $value['id'] ) ); 
    } 
}
?>
<?php get_header(); ?>

<?php /* Enables two or three columns */
if ($et_threecolumn_disable == "false") { ?> <?php include(TEMPLATEPATH."/sidebar.php");?><?php } ?>

<div class="content <?php if ($et_threecolumn_disable == "false") { ?> <?php echo $et_columnorder; ?> <?php } else { ?> content-two-column<?php echo $et_columnorder; ?> <?php } ?>">
<?php
$temp = $wp_query;

$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);

wp_reset_query();
wp_reset_postdata();

$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);



if (have_posts()) : while (have_posts()) : the_post();
    $video = get_post_custom_values('YT_video_id');
?>

<div class="post" id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    <h2><?php the_time('F jS, Y') ?> | <?php the_category(', ') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h2>

    <div class="entry">
        <?php the_content('Read More'); ?>

        <iframe id="player" type="text/html" width="600" height="355" src="http://www.youtube.com/embed/<?php echo $video[0] ?>" frameborder="0"></iframe>

        <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    </div>

    <?php if (function_exists('the_tags')) { the_tags('<p class="postmetadata">Tags: ', ', ', '</p>'); } ?>
</div>

<?php endwhile; endif; ?>


<div class="navigation">
    <div class="alignleft"><?php next_posts_link('&larr; Previous Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Next Entries &rarr;') ?></div>
</div>
<?php wp_reset_query(); $wp_query = null; $wp_query = $temp;?>

</div>


<?php include(TEMPLATEPATH."/primary-sidebar.php");?>

<?php get_footer(); ?>  
4

1 回答 1

1

我进行了一些测试。

$temp = $wp_query;

$number_of_posts_per_page = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = 3 + (($paged > 1) ? $number_of_posts_past : 0);

wp_reset_query();
wp_reset_postdata();

$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);

$wp_query->found_posts; // 12

所以found_posts 似乎忽略了offset 和posts_per_page。而且由于根据法典 max_num_pages 计算为 $found_posts / $posts_per_page 它出现了 3 页。

我需要计算我自己的 max_pages 以提供给分页链接。

$temp = $wp_query;

$number_of_posts_per_page = 5;
$initial_offset = 3;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = $initial_offset + (($paged > 1) ? $number_of_posts_past : 0);

wp_reset_query();
wp_reset_postdata();

$wp_query = new WP_Query('category_name=efforts&posts_per_page='.$number_of_posts_per_page.'&offset='.$off);

$max_pages = ceil(($wp_query->found_posts - $initial_offset) / $number_of_posts_per_page);

// snipped a bunch of post related code that worked fine

<div class="navigation">
    <div class="alignleft"><?php next_posts_link('&larr; Previous Entries', $max_pages) ?></div>
    <div class="alignright"><?php previous_posts_link('Next Entries &rarr;', $max_pages) ?></div>
</div>
于 2013-01-29T17:48:38.983 回答