我正在做我的第一个WP项目。
我有一个主题,把所有的帖子都贴在主页上--我需要每5-6个帖子添加一个重复的广告。
我的想法是更改列出帖子的数据库查询,并每隔这么多循环添加广告。
有人能指点我在哪里找到数据库查询吗?
还是有一个更优雅的解决方案,你可以建议?
谢谢。
发布于 2018-04-06 17:07:30
可能只是添加一个计数器,在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;发布于 2018-04-17 05:30:03
菲尔·库思写了一篇内容丰富的文章使用WordPress对象内的current_post属性拆分处理global $wp_query循环。
这可以应用于您的问题,并允许我们在循环中的任何点插入内容。
该函数如下(放置在functions.php中,或者,如我所愿,放入一个单独的库文件,该文件只处理查询mods ):
/**
* 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篇文章之后插入广告:
if ( have_posts() ) :
while (
have_posts() ) :
the_post();
get_template_part( 'content' );
if ( pdk_get_current_loop_index() === 6 ) {
?>如果你读了菲尔的文章,你还可以用这个函数做更多的事情。
https://wordpress.stackexchange.com/questions/300029
复制相似问题