我想向十分之一的人展示某些Wordpress帖子。我猜这将是一个JS解决方案,它只是使用随机数生成器隐藏以发布。
有没有人曾经遇到过类似的情况,并愿意展示他们是如何解决的?
我想向十分之一的人展示某些Wordpress帖子。我猜这将是一个JS解决方案,它只是使用随机数生成器隐藏以发布。
有没有人曾经遇到过类似的情况,并愿意展示他们是如何解决的?
function lsmwp_post_limter($ids, $in, $count) {
$i = rand(1,$count);
if ($i <= $in) {
return($ids);
}
else
return false;
}
function lsmWpfilter($posts){
$result = [];
foreach($posts as $post){
$res = lsmwp_post_limter($post[\'postId\'], $post[\'in\'], $post[\'count\']);
if ($res){
$result[] = $res;
}
}
print_r($result);
return $result;
}
$exluded_posts = array(
array(\'postId\' => 28741 , \'in\' => 2, \'count\' => 3),
array(\'postId\' => 29811 , \'in\' => 2, \'count\' => 3),
array(\'postId\' => 31951 , \'in\' => 1, \'count\' => 3)
);
$custom_args = array(
\'post__not_in\' => lsmWpfilter($exluded_posts),
);
这种方法允许我对每个被排除的帖子使用不同的in/count,而不是将其限制为1/5,也可以使用函数假设您已经有了要随机显示的特定帖子ID(每10个1个),您可以使用:
while ( have_posts() ) : the_post();
if($post->ID==100 OR $post->ID==101){
if (rand(1,10)==5) {the_content();}
else
the_content();}
endwhile;
请注意,此代码不计算访问者数量,并且在每次第10个访问者访问页面时显示帖子。为此,需要创建一个变量并将计数器值存储在数据库中。然而,这将服务几乎像你想要的方式。我需要每两篇文章显示一个不同的布局,是否可以使用偶数/奇数来实现这一点?<?php while (have_posts()): the_post() ?> <?php if ($wp_query->current_post % 2 == 0): ?> even <?php else: ?> odd <?php endif ?> <?php endwhile