【问题标题】:Insert code between 3rd post in wordpress在 wordpress 的第三篇文章之间插入代码
【发布时间】:2015-10-16 04:32:06
【问题描述】:

我无法在 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>

谢谢。

【问题讨论】:

    标签: php ads wordpress


    【解决方案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; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-30
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多