在wordpress中的第3个帖子之间插入代码

时间:2015-07-25 16:17:48

标签: php wordpress ads wordpress-theme-customize

我无法在Wordpress中的每3个帖子后插入广告。这是主题中的代码。我知道我需要一个计数器和一个if语句。

<section id="recentnews">
        <div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
        <?php $args = array(
               'posts_per_page' => '5',
               'offset' => '5',
               'ignore_sticky_posts' => '1'
                );
        ?>
        <?php $query = new WP_Query($args); ?>
        <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
        <article class="post">
            <div class="row">
                <div class="five columns">
                    <div class="post-gallery">
                        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('recent'); ?></a>
                        <?php echo thb_DisplayImageTag(get_the_ID()); ?>
                    </div>
                </div>
                <div class="seven columns">
                    <div class="post-title">
                        <aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
                        <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                    </div>
                    <div class="post-content">
                        <p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
                        <?php echo thb_DisplayPostMeta(true,true,true,false); ?>
                    </div>
                </div>
            </div>
        </article>
        <?php endwhile; else: ?>
        <article>
            <?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
        </article>
        <?php endif; ?>
        <a id="loadmore" href="#" data-loading="<?php _e( 'Loading ...', THB_THEME_NAME ); ?>" data-nomore="<?php _e( 'No More Posts to Show', THB_THEME_NAME ); ?>" data-count="5" data-action="thb_ajax_home"><?php _e( 'Load More', THB_THEME_NAME ); ?></a>
    </section>

我可以告诉你我尝试过但失败了,只是为了表明我尝试过。请不要给我负面评价。

    <section id="recentnews">
            <div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
            <?php $args = array(
                   'posts_per_page' => '5',
                   'offset' => '5',
                   'ignore_sticky_posts' => '1'
                    );
            ?>
            <?php $query = new WP_Query($args); ?>

            <?php $i = 1;  if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if($i == 1) :  ?>

            <article class="post">
                <div class="row">
                    <div class="five columns">
                        <div class="post-gallery">
                            <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('recent'); ?></a>
                            <?php echo thb_DisplayImageTag(get_the_ID()); ?>
                        </div>
                    </div>
                    <div class="seven columns">
                        <div class="post-title">
                            <aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
                            <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                        </div>
                        <div class="post-content">
                            <p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
                            <?php echo thb_DisplayPostMeta(true,true,true,false); ?>
                        </div>
                    </div>
                </div>

            </article>
            <div class="clear"> </div>  
        <?php if ( $i == 3||  $i == 9 || $i == 15 ) : ?>
            <?php if (function_exists ('adinserter')) echo adinserter (2); ?>
            <div class="clear"> </div>
        <?php endif; ?><?php endif; ?>

<?php $i++; ?>
            <?php endwhile; else: ?>
            <article>
                <?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
            </article>
            <?php endif; ?>
        </section>

谢谢。

1 个答案:

答案 0 :(得分:1)

你很亲密。您需要在while循环内增加计数器(使用$i++),然后使用modulus operator %来确定计数器是否可被整除3:

<?php if ($query->have_posts()) : $i = 1; while ($query->have_posts()) : $query->the_post(); ?>

    <!-- Article -->

    <?php if ( $i % 3 == 0 ) : ?>
        <!-- Advertisement Here -->
    <?php endif; ?>

<?php $i++; endwhile; else: ?>
    <!-- Display Notice -->
<?php endif; ?>