Php 在wordpress主页的每第7篇文章后插入广告

Php 在wordpress主页的每第7篇文章后插入广告,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我试图在我的主页和wordpress网站的分类页面中每7篇文章之间放置谷歌广告 我在WordPress论坛上找到了这段代码,并尝试了一下,结果发现它不起作用。我不知道如何在我的主题中使用此代码。 我找到的代码是这样的 if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post(); //before if (($count>1) && ($count%5 == 0) ){ ?> <

我试图在我的主页和wordpress网站的分类页面中每7篇文章之间放置谷歌广告

我在WordPress论坛上找到了这段代码,并尝试了一下,结果发现它不起作用。我不知道如何在我的主题中使用此代码。 我找到的代码是这样的

if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post();
//before 
if (($count>1) && ($count%5 == 0) ){ ?>
  <div>
    [adcode] 
  </div> <?
          } $count++;
if(have_posts()):$count=0;while(have_posts()):the_post();
//以前
如果($count>1)&($count%5==0)){?>
[广告代码]

我想知道如何根据index.php的上部代码放置代码。谢谢

您可以做的是创建一个$计数器,然后在此基础上显示adcode。因此,您的最终代码将是:

$counter = 1;
if(have_posts()) : while(have_posts()) : the_post(); ?>

<?php 
if($counter % 7 == 0) {
?>
[adcode] 
<?php
}

if ( floatval(get_bloginfo('version')) < "3.6" ) {
    //old post formats before they got built into the core
     get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() ); 
} else {
    //WP 3.6+ post formats
     get_template_part( 'includes/post-templates/entry', get_post_format() ); 
} ?>

<?php $counter++; endwhile; endif; ?>
$counter=1;
if(have_posts()):while(have_posts()):the_post();?>
[广告代码]

我们只需增加$counter,并在每七篇文章后显示[adcode]

我假设[adcode]是一个短代码,原因是方括号。此代码未经测试

if(have_posts()) : $count = 0; while(have_posts()) : the_post(); ?>

<?php 

if ( $count % 7 == 0 ) {
    do_shortcode('[adcode]');
}

if ( floatval(get_bloginfo('version')) < "3.6" ) {
//old post formats before they got built into the core
    get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() ); 
} else {
//WP 3.6+ post formats
    get_template_part( 'includes/post-templates/entry', get_post_format() ); 
} ?>

<?php $count++; endwhile; endif; ?>
if(have_posts()):$count=0;while(have_posts()):the_post();?>

这只适用于前7篇文章。我的意思是1,2,3,4,5,6,7[ad]9,10,11,12,13,14,15,16…….为什么在第14篇文章后没有显示广告。奇怪的是,应该可以工作,下一篇文章,因为从7到7,计数器的模将为0。可能有分页的问题,你每页有多少篇文章?每页有10篇文章,我使用的是无限卷轴。每个卷轴加载10篇文章。所以这就是为什么它不起作用,它通过ajax加载下一篇文章。解决方案是在下一篇文章中使用jQuery或javascript添加ads代码,或者找到返回结果的php调用,然后在那里添加代码。我很难理解,所以我将把它留在这里。谢谢你的帮助。保持快乐和幸福。[adcode]不是短代码。这意味着广告代码将在这里。您的代码给出了此错误解析错误:语法错误,第96行的C:\wamp\www\wp\wp content\themes\sential\index.php中出现意外的“endwhile”(T_endwhile)。第96行是
if(have_posts()) : $count = 0; while(have_posts()) : the_post(); ?>

<?php 

if ( $count % 7 == 0 ) {
    do_shortcode('[adcode]');
}

if ( floatval(get_bloginfo('version')) < "3.6" ) {
//old post formats before they got built into the core
    get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() ); 
} else {
//WP 3.6+ post formats
    get_template_part( 'includes/post-templates/entry', get_post_format() ); 
} ?>

<?php $count++; endwhile; endif; ?>