2

我已经在网上阅读了一些关于他的问题的信息,但我没有认真的解决方案。我阅读的资源: http:
//codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

http://wordpress.org/support/topic/next_posts_link-not-displays-same-posts-not-next-posts-when-using-offset - 完全相同的问题。

现在这是我在 index.php 中的代码:

<!--slider-->
<div id="featured"> 
            <?php 
                // query the posts of your custom post types
                query_posts('posts_per_page=5');
                if (have_posts()) : while (have_posts()) : the_post(); ?>

<!--main body-->
    <?php 
                // query the posts of your custom post types
                query_posts('posts_per_page=5&offset=5');
                if (have_posts()) : while (have_posts()) : the_post(); ?>

当我按下下一个帖子时,我会得到相同的正文帖子副本。
解决方案是什么?

编辑: 我写了新代码:

// featured
$slider = new WP_Query(array('posts_per_page' => 5));
if ($slider->have_posts()) : while ($slider->have_posts()) : $slider->the_post();

//main:
    $p = get_query_var('paged');
                if($p >0){
                $main = new WP_Query(array('posts_per_page' => 5, 'paged'=>get_query_var('paged')));
                }else{
                $main = new WP_Query(array('posts_per_page' => 5, 'offset' => 5,'paged'=>get_query_var('paged')));
                }
                if ($main->have_posts()) : while ($main->have_posts()) : $main->the_post();

//导航:

<div class="navigation" >
    <div class="next-posts"><div class="nextBtn2"><?php previous_posts_link(); ?></div></div>
    <div class="prev-posts"><?php next_posts_link(); ?></div>
</div>
4

4 回答 4

1

您应该将 WP_Query 用于辅助循环(特色)并将 query_posts 用于主体。
它工作 95%
它将在最后一页中断,因为它会尝试获取(非常)最后 5 个项目,但那 5 个将在偏移量中。:(
因此,在倒数第二页上,您需要隐藏“下一页”分页....

$featured = new WP_Query(array('posts_per_page' => 5));
if ($featured->have_posts()) : while ($featured->have_posts()) :
    $featured->the_post();

//shouldn't be needed if u use only one query_posts but it won't hurt
wp_reset_query();

$paged = get_query_var('paged');
$wanted_offset = 5;
// page zero won't need a hack
if ($paged != 0 ) {
    $post_per_page = 3;
    //$paged -1 because there is no page 1, just 0 and 2
    $offset = ($paged-1) * $post_per_page + get_query_var('posts_per_page');
} else {
    $offset = get_query_var('posts_per_page');    
}
query_posts('offset=' . $offset);
if (have_posts()) : while (have_posts()) : the_post();

让我知道它是否也适合你。

于 2012-04-12T13:51:26.910 回答
0

我在制作wordpress主题时已经遇到了这个问题,

实际上,当您使用时,有很多方法可以显示 post 循环

query_posts,您可能需要在循环后重置查询

<?php

// The Query
query_posts('posts_per_page=5');   
// The 1st Loop
while ( have_posts() ) : the_post();
    //your first set of latest posts
endwhile;  
// Reset Query
wp_reset_query();

//Second Query
query_posts(array('paged'=>$paged, 'offset'=>5));
// The 2nd Loop
while (have_posts()) : the_post();
    // your second set of posts
endwhile;
// Reset Query
    wp_reset_query();

?>

您也可以在第二个循环中使用 get post(这个 wp 函数有一个偏移参数)

<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>

http://codex.wordpress.org/Template_Tags/get_posts

如果您已经在滑块上发布了帖子,则可以在正文的第二个循环中使用它。

希望能帮助到你

于 2012-04-12T06:34:00.993 回答
0
<?php if(is_home() && !is_paged()): ?>

?>

<?php else:
$setpo = get_option('posts_per_page');
$targ = $setpo*($paged-2)+11; ?>

<?php query_posts(array('category_name' => 'websites', 'offset' => $setpo));
endif; ?>
于 2012-05-30T13:35:43.977 回答
0

您是否尝试过使用 WP_Query 对象的两个实例?

<?php

// featured
$featured = new WP_Query(array('posts_per_page' => 5));
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();

// main body
$main = new WP_Query(array('posts_per_page' => 5, 'offset' => 5));
if ($main->have_posts()) : while ($main->have_posts()) : $main->the_post();

?>
于 2012-04-10T05:39:03.443 回答