在帖子的网格循环之间插入Javascript(Adsense)


Inserting Javascript (Adsense) between a grid loop of posts?

我的网站dadgab.com(Wordpress)有一个包含4行的2列循环

页眉
X X
X X
X X
页脚

我想把adsense javascript插入到第三个帖子所在的位置(左中)。经过一周的努力,我希望这里有人能帮助我

以下是我在index.php中的循环:

            <?php while ( have_posts() ) : the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class('box'); ?>>
                <div class="post-image">
                    <?php
                        $thumb = get_post_thumbnail_id();
                        $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
                        $image = aq_resize( $img_url, 300, 150, true ); //resize & crop the image
                    ?>
                    <?php if($image) : ?>
                        <a href="<?php the_permalink(); ?>"><img width="300" height="150" src="<?php echo $image ?>" alt="<?php the_title() ?>" /></a>
                    <?php endif; ?>
                </div>


我尝试了添加各种方法,包括将以下文本添加到functions.php,并添加一个空白帖子,标记为"adsense"。这只会产生一个空框,不过如果我真的点击帖子,广告就会显示出来。

    add_filter('the_content', 'replace_adsense_posts_excerpt');
function replace_adsense_posts_excerpt($excerpt)
{
    $post = get_post(get_the_ID());
    if (has_tag('adsense ', $post)) {
        // Replace the content with our adsense code
        $excerpt = my_adsense_code();
    }
    return $excerpt;
}
function my_adsense_code()
{
    $adsense = 'ADSENSE CODE';
    return $adsense;
}

只需在循环中添加一个计数器,并在计数器等于3:时输出代码

<?php $i=0; while ( have_posts() ) : the_post(); $i++;?>
     <?php if($i==3):?>
         <div <?php post_class('box'); ?>>ADSENSE CODE</div>
     <?php endif;?>
     <article id="post-<?php the_ID(); ?>" <?php post_class('box'); ?>>
         ....

您需要使用css来确保adsense与文章

的维度相同