我目前正在第一次在WordPress
网站上工作,并且在解决如何实现我的目标方面遇到了一些问题。我希望它能够在每三个帖子之后显示一个横幅广告,然后从我现有帖子的中断处继续。
因为我可能措辞不好,所以我会在下面显示一个示例图片。
这是我在index.php
文件中使用的当前循环文件。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#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; ?>
答案 0 :(得分:1)
你可以这样做: -
<?php $i = 0; // create a counter?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#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 :(得分:1)
试试这个......
<?php
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#8211;", $title);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
if($counter == 3){ ?>
********** Advertisement code write here ***********
<?php
}
?>
<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
$counter++;
endwhile; else : ?>