我已经为此工作了很多很多很多周了。我已经在这里和其他地方问过类似的问题,但似乎没有人能够解决这个问题。也许无论如何都不可能?
我的相关帖子是基于标签的,代码如下所示:
<?php
$backup = $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'showposts\'=>4, // Number of related posts that will be shown.
\'caller_get_posts\'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) {
$my_query->the_post();
?>
我希望用户能够滚动到接下来的4篇相关帖子,然后再滚动到接下来的4篇。共12个员额。我正在使用jQuery工具的滚动功能来实现这一点。
每个循环将位于div之间,以便scrollable可以在何处剪切内容以及在何处滚动。
我所能找到的关于抵消你的循环的方法是:
// FIRST LOOP: display posts 1 thru 4
<?php query_posts(\'showposts=4\'); ?>
<?php $posts = get_posts(\'numberposts=4&offset=0\'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "4") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
// SECOND LOOP: display posts 5 thru 8
<?php query_posts(\'showposts=5\'); ?>
<?php $posts = get_posts(\'numberposts=4&offset=4\'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "4") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
// THIRD LOOP: display posts 9 thru 12
<?php query_posts(\'showposts=4\'); ?>
<?php $posts = get_posts(\'numberposts=4&offset=8\'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "4") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count3++; } ?>
<?php endforeach; ?>
我试过了,但一直收到错误消息。基本上,我只是不知道如何将我的原始代码与该代码结合起来。
还是有更优雅的解决方案?
EDIT: UPDATE October 17th
代码基本正常,只有一个大问题。它现在在每个条目中生成相同的相关帖子,甚至在没有任何标签的帖子中!!而且,它也会把评论弄得一团糟!它不会显示正确数量的评论,而是显示另一篇文章的pingback。知道为什么吗?
<h2>Related Posts</h2>
<!-- "previous page" action -->
<a class="prev browse left"></a>
<?php
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'showposts\'=>12, // get all the posts at once, then split them up afterwards.
\'ignore_sticky_posts\' => 1
);
$posts_per_block = 4;
$my_query = new wp_query($args); ?>
<!-- root element for scrollable -->
<div class="scrollable" id=chained>
<!-- root element for the items -->
<div class="items">
<?php
if( $my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post();
if ( ($my_query->current_post) == 0 ) {
echo \'<div>\';
}
elseif ( ( $my_query->current_post ) % $posts_per_block == 0 ) {
echo \'</div><div>\'; //close and open a new div every nth post
}
elseif ( ( $my_query->current_post + 1) == ( $my_query->post_count ) ) {
echo \'</div>\'; //close the div if you run out of posts
} ?>
<div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
<div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>
<?php
endwhile; endif;
?>
</div>
</div>
<!-- "next page" action -->
<a class="next browse right"></a>
我真的希望最终有人能帮我解决这个问题。
提前非常感谢。
最合适的回答,由SO网友:djb 整理而成
您的意思是要显示四个相关帖子的三个块,并使用滚动条在三个块之间切换?
如果是,以下内容是否有帮助(未测试)?假设scrollable需要将其内容放在单独的div中,您可以这样做。它应该生成一个包含最多三个div的div,每个div包含4个post。。。假设您有12篇文章要显示。如果没有,它应该优雅地死去,而不留下任何悬空的div。
<?php
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'showposts\'=>12, // get all the posts at once, then split them up afterwards.
\'ignore_sticky_posts\' => 1
);
$posts_per_block = 4;
$my_query = new wp_query($args); ?>
<div id="scroller-wrapper">
<?php
if( $my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post();
if ( ($my_query->current_post) == 0 ) {
echo \'<div class="scroller-block">\';
}
else if ( ( $my_query->current_post ) % $posts_per_block == 0 ) {
echo \'</div><div class="scroller-block">\'; //close and open a new div every nth post
}
the_title();
the_content(); //any other post stuff here
if ( ( $my_query->current_post + 1) == ( $my_query->post_count ) ) {
echo \'</div>\'; //close the div if you run out of posts
}
endwhile; endif;
?>
</div>
NB 您在示例中使用了一些不推荐使用的代码,例如“caller\\u get\\u posts”和wp\\u start()-您应该分别使用“ignore\\u sticky\\u posts”和\\u post()。
EDIT: 将内部div的结尾移到了post内容之后,而不是之前。哎呀