dongyinshua9996 2016-06-26 09:14
浏览 14
已采纳

每三个帖子后放置替代文本

I am currently working on a WordPress site for the first time and having some issues figuring out how to achieve what I am aiming for. I would like for it to display a banner advertisement after every third post then continue from where it left off in my existing post.

Because I probably worded this poorly I will display an example image below.

Here's the current loop file I am using within my index.php file.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

<?php 

    $title = htmlentities(get_the_title ()); 
    $str = explode ("&amp;#8211;", $title);

    $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
    $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);

?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
    <div class="cover">
        <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a>
    </div>
<div class="metadata">
    <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
    <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
    </a>
    <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding:  5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a>
</div>
</div>
<?php endwhile; else : ?>



<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>



<?php endif; ?>
  • 写回答

2条回答 默认 最新

  • donqh00404 2016-06-26 09:37
    关注

    You can do it like below:-

    <?php $i = 0; // create a counter?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 
    
    <?php 
    
        $title = htmlentities(get_the_title ()); 
        $str = explode ("&amp;#8211;", $title);
    
        $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
        $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
    
    ?>
    <div class="album-meta" style="border-bottom: 1px solid #eee;">
        <div class="cover">
            <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a>
        </div>
        <div class="metadata">
            <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
            <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
            </a>
            <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding:  5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a>
        </div>
    </div>
    <?php if($i%3 ==0 && $i >0){ // check you reached to third div or not?>
    <!-- write the html of advertisement div ------->
    <?php $i++;} ?> <!-- increase counter -->
    <?php endwhile; else : ?>
    
    
    
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
    
    
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值