如何在每个“X”个帖子后显示其他内容(例如广告)

时间:2017-09-10 14:43:33

标签: php wordpress adsense

我有Wordpress网站,我想在其中实施Adsense广告。

我每页有30个帖子,所以我想在每7个帖子后显示广告,我该怎么做?目前我在10个帖子中使用此方法进行了3个广告,在10个帖子之后没有广告显示:

<center><?php if( $wp_query->current_post == 1 ) : ?>

Adsense Code Here 
<?php elseif( $wp_query->current_post == 3 ) : ?>

Adsense Code Here
<?php elseif( $wp_query->current_post == 7 ) : ?>

Adsense Code Here
<?php endif; ?></center>

我想在每7个帖子后展示广告,这可能在一个代码行中吗?

2 个答案:

答案 0 :(得分:0)

你只需要在这里放一个条件: -

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(

actionButton("buttonresinvthree", "Resource Activity",style="color: 
#000000; width:8%;white-space:normal;font-size:.8vw;")
)
)
server <- function(input, output) { }
shinyApp(ui, server)

通过这个,你会得到0作为提醒,每个帖子计数是7的倍数,如7,14,21等。

答案 1 :(得分:0)

您需要使用模数(或&#34; mod&#34;)运算符%来获取值x的余数除以值y,即{{1} }。例如x % y = remainder因为4除以3得到余数1。


您的代码应为:

4 % 3 = 1


这是如何运作的:

您希望在每7个帖子之后显示广告,因此您需要使用3作为<?php if( ($wp_query->current_post % 7) == 1 ) : ?> Adsense Code Here <?php endif; ?> ,即要除以的值。这将给出结果:

y

如果你想在第一个广告之后开始,那么你想要检查余数值是否为1.


提示:

关闭主题,但已弃用 1st post: 1 % 7 = 1 2nd post: 2 % 7 = 2 3rd post: 3 % 7 = 3 [...] 6th post: 6 % 7 = 0 7th post: 7 % 7 = 1 8th post: 8 % 7 = 2 [...] 14th post: 14 % 7 = 1 etc. HTML标记,因此您不应再使用它。改为在容器元素上使用CSS样式<center>