我正在做我的第一个WP项目。
我有一个主题,平铺整个主页上的所有帖子-我需要每5-6篇帖子添加一个重复广告。
我的想法是更改列出帖子的数据库查询,并每隔这么多个循环添加一次广告。
有人能告诉我在哪里可以找到数据库查询吗?
或者你能提出一个更优雅的解决方案吗?
非常感谢。
我正在做我的第一个WP项目。
我有一个主题,平铺整个主页上的所有帖子-我需要每5-6篇帖子添加一个重复广告。
我的想法是更改列出帖子的数据库查询,并每隔这么多个循环添加一次广告。
有人能告诉我在哪里可以找到数据库查询吗?
或者你能提出一个更优雅的解决方案吗?
非常感谢。
可能只是添加一个计数器,以6的倍数显示广告。
类似于
$count = 0;
$adEvery = 6;
if (have_posts()) :
while (have_posts()) : the_post();
// Individual Post
$count++;
if ($count%$adEvery == 0) {
// your ad
}
endwhile;
else :
// No Posts Found
endif;
Phil Kurth has written an informative article 使用current_post
中的属性global $wp_query
对象
这可以应用于您的问题,并允许我们在循环中的任意点干净地插入内容。
功能如下所示(置于functions.php
或者,如我所愿,放入一个单独的库文件,该文件只处理查询mod):
/**
* Returns the index of the current loop iteration.
*
* @return int
*/
function pdk_get_current_loop_index() {
global $wp_query;
return $wp_query->current_post + 1;
}
然后在输出循环时,如果我们想在第6个帖子之后插入ad:if ( have_posts() ) :
while (
have_posts() ) :
the_post();
get_template_part( \'content\' );
if ( pdk_get_current_loop_index() === 6 ) {
?>
<div class="ad-mrec">
<!-- Insert ad coder here -->
</div>
<?php
}
endwhile;
endif;
如果您阅读Phil的文章,还可以使用此函数执行更多操作。我想在meta\\u查询中添加多个值。我在这个链接中找到了解决方案:meta_query with multiple values代码如下:$meta_query_args = [ [ \'key\' => \'checkboxes\', \'value\' => [ \'\"facebook\"\', \'\"twitter\"\' ], \'compare\' => \'LIKE\',