我是新来的,我想在wordpress的编码方面得到你的帮助。重点是我想在项目之间做广告,如下所示:
<div class="row top-articles">
<?php
$args= array(
\'posts_per_page\'=>4,
\'post__not_in\'=> $excluded
);
$i=0;
$otherLoop = new WP_Query( $args );
while ($otherLoop -> have_posts() ) : $otherLoop->the_post();$i++;
$cat = get_the_category($post->ID)[0];
array_push($excluded,$post->ID);
?>
<div class="small-6 top-story-container columns medium-6 large-3">
<article class="artikulli-<?php echo$cat->slug; ?> live">
<a href="<?php the_permalink();?>">
<div class="img-container">
<div class="inner">
<div class="gradient"></div>
<?php if($videos = get_post_meta($post->ID, \'ecpt_videos\',\'ecpt_foto\', true)): ?>
<i class="icon dotie-icon_video dotie-icon_gallery phone-medium tablet-large desktop-medium"></i>
<?php endif; ?>
<?php if($foto = get_post_meta($post->ID, \'ecpt_foto\', true)): ?>
<i class="icon dotie-icon_gallery phone-medium tablet-large desktop-medium"></i>
<?php endif; ?>
<?php the_post_thumbnail(\'250x140\'); ?>
</div>
</div>
<div class="article-meta equalise">
<h3>
<span class="underline">
<?php if($live = get_post_meta($post->ID, \'ecpt_live\', true)): ?>
<span class="live">Live</span>
<?php endif; ?>
<?php echo wp_trim_words( get_the_title(), 7 );?>
</span>
</h3>
<span class="datetime primary-color">
<?php echo $cat->cat_name; ?>
</span>
</div>
<div class="clearfix"></div>
</a>
</article>
</div>
<?php endwhile; ?>
</div>
</div>
<div class="clearfix"></div>
SO网友:Carlos Faria
当您想要停止放置广告时,请使用WP\\u Query,然后使用break循环结果。
$args = [
\'posts_per_page\' => 5
];
$posts = new WP_Query($args);
然后循环2
<section class="posts">
<?php $cont = 0; ?>
<?php
while( $posts->have_posts() ) : $posts->the_post(); $cont++;
$id = get_the_ID();
//Show your post here
if($cont >= 1) break;
endwhile;
?>
</section>
<div class="ad">
<?php //Show ad ?>
</div>
<?php //Rest of the posts ?>
<section class="posts">
<?php
while( $posts->have_posts() ) : $posts->the_post();
$id = get_the_ID();
//Show your post here
endwhile;
?>
</section>
如果在帖子或页面中执行此操作,则应在显示帖子后重置查询:
wp_reset_postdata();