我需要我的网站主页有3列:-第一列有5个浏览量最多的帖子;-第二个是接下来5个浏览量最大的帖子;-第三个是最近发布的帖子。
我不希望帖子永远不会在同一列或另一列中重复,因此我使用wp\\u query以我想要的数量获取我想要的帖子。下面是我加载第二页时发生的情况,使用简单分页或无限滚动:
第一个循环加载5-10个查看次数最多的帖子,这些帖子已经显示在第二个循环的第一次运行中第二个循环加载接下来5个浏览量最大的帖子,其中一些帖子在第一次运行时已经加载到第三个循环中第三个循环加载最近的帖子,而第一个或第二个循环中没有加载这些帖子当我仍在加载下一页时,循环会加载相应的帖子,直到我在第三个循环中获得任何帖子,然后在第二个循环中没有帖子,并将所有帖子加载到第一个循环中。
下面是我使用的代码:
<div id="content">
<div class="recentColumn">
<div class="columnContent">
<?
$hotArgs = array(
\'post__not_in\' => $loaded,
\'meta_key\' => \'wpb_post_views_count\', // Pega o número de visitas do post.
\'orderby\' => \'meta_value_num\', // Ordena do mais visitado para o menos visitado.
\'posts_per_page\' => \'2\',
\'paged\' => $paged
);
$hot = new WP_Query( $hotArgs );
if ($hot->have_posts()) : while ($hot->have_posts()) : $hot->the_post();
$loaded[] = $post->ID;
?>
<div class="articleContent">
<article class="recent" role="article">
<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_category();?>
<header>
<h1><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h1>
<?php the_excerpt(); ?>
</header>
</article>
</div>
<?php endwhile; endif; ?>
</div>
</div>
<div class="popularColumn">
<div class="columnContent">
<?
$nextArgs = array(
\'post__not_in\' => $loaded,
\'meta_key\' => \'wpb_post_views_count\', // Pega o número de visitas do post.
\'orderby\' => \'meta_value_num\', // Ordena do mais visitado para o menos visitado.
\'posts_per_page\' => \'3\',
\'paged\' => $paged
);
$next = new WP_Query( $nextArgs );
if ($next->have_posts()) : while ($next->have_posts()) : $next->the_post();
$loaded[] = $post->ID;
?>
<div class="articleContent">
<article class="popular" role="article">
<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_category();?>
<header>
<h1><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h1>
</header>
</article>
</div>
<?php endwhile; endif; ?>
</div>
</div>
<div class="randomColumn">
<div class="columnContent">
<?
$recentsArgs = array(
\'post__not_in\' => $loaded,
\'posts_per_page\' => \'3\',
\'paged\' => $paged
);
$recents = new WP_Query( $recentsArgs );
if ($recents->have_posts()) : while ($recents->have_posts()) : $recents->the_post();
$loaded[] = $post->ID;
?>
<div class="articleContent">
<article class="random" role="article">
<header>
<h1><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h1>
</header>
</article>
</div>
<?php endwhile; endif; ?>
</div>
</div>
我认为解决方案可以使用一个php会话或变量来存储已加载帖子的id,并在循环中检查帖子是否已加载,然后再继续。我试着做这个,但没有成功。我在php方面的技能太少了,也许有人会对如何使其工作有更好的想法。
我感谢你的帮助。谢谢
SO网友:bonger
您可以重新创建$loaded
数据库中的数组:
$hot_ppp = 3;
$next_ppp = 3;
$recents_ppp = 3;
if ( $paged > 1 ) {
$prev_hot_next_loaded = $wpdb->get_col( $wpdb->prepare(
\'SELECT ID FROM \' . $wpdb->posts . \' p JOIN \' . $wpdb->postmeta . \' pm ON pm.post_id = p.ID AND pm.meta_key = %s\'
. \' WHERE p.post_type = %s AND p.post_status = %s ORDER BY pm.meta_value+0 DESC LIMIT %d\',
\'wpb_post_views_count\', \'post\', \'publish\', ( $hot_ppp + $next_ppp ) * ( $paged - 1 )
) );
$prev_recents_loaded = $wpdb->get_col( $wpdb->prepare(
\'SELECT ID FROM \' . $wpdb->posts . \' WHERE ID NOT IN (\' . implode(\',\', $prev_hot_next_loaded ) . \')\'
. \' AND post_type = %s AND post_status = %s ORDER BY post_date DESC LIMIT %d\',
\'post\', \'publish\', $recents_ppp * ( $paged - 1 )
) );
$loaded = $prev_hot_next_loaded + $prev_recents_loaded;
}