وقف المشاركات المكررة
-
29-09-2020 - |
سؤال
لدي ثلاث حلقات أريد منعهم من عرض نفس المشاركات في كل واحدة.
أي مساعدة سيكون موضع تقدير كبير
<?php
$i;
$args = array( 'numberposts' => 8);
$posts= get_posts( $args );
if ($posts) {
foreach ( $posts as $i => $post )
{
setup_postdata($post);
}
}
?>
<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div><!-- end item -->
<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div>
<div class="item" id="<?php echo $i+1; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div>
المحلول 2
giveacodicetagpre.
هذا هو إصلاحي في النهاية.
نصائح أخرى
يمكنك استخدام WP_Query
لإنشاء الحلقة المخصصة الخاصة بك، ثم استخدم offset
المعلمة لتخطي المشاركات في الحلقة السابقة.ستقوم بتشغيل 3 حلقات من استعلام واحد، لذا كل ما عليك فعله لإعادة تشغيل الاستعلام هو الاستخدام rewind_posts()
بعد كل حلقة لإعادة ضبط الحلقة
فيما يلي مثال للكود الذي يمكنك تعديله ليناسب احتياجاتك المحددة.هذه مجرد حلقة أساسية جدًا
<?php
$args = array( 'posts_per_page' => 4);
$my_query= new WP_Query( $args ); ?>
<ul>
<?php
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
$my_query->rewind_posts();
?>
<ul>
<?php
$my_query->query('posts_per_page=4&offset=4');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
$my_query->rewind_posts();
?>
<ul>
<?php
$my_query->query('posts_per_page=4&offset=8');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
wp_reset_postdata();
?>
لا تنتمي إلى wordpress.stackexchange