[英]Seprate sticky posts from main loop. #WordPress
我需要你的幫助。 我想將粘性帖子與主循環(模板:index.php)分開,並想在剩余循環之前的粘性帖子之后添加一些內容。 請看截圖。 這個自定義循環有什么提示嗎? 我會很感激的。
截圖: http : //imgur.com/a/e37Pj
結構是這樣的:
— These are sticky posts
– sticky post one
– sticky post two
– sticky post three
++ Add something after sticky posts.
— The rest of main loop.
– normal post one
– normal post two.
– normal post three.
提前致謝。
問候 – 波米
在索引中的標准循環之前,您創建區域創建要加載“粘性”帖子的位置
在帖子頁面寫這個
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 'Sticky' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
這將為您提供您想要的類別的帖子,在這種情況下,您將從“粘性”類別中獲得5個帖子
試試這個:
<?php
$args = array('posts_per_page' => 12, 'post__in' => get_option('sticky_posts'));
$stickyposts = new WP_Query($args);
while($stickyposts->have_posts()) : $stickyposts->the_post();
?>
<!-- sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- -->
<!-- here you can add random text or a horizontal line with a new headline or s.th.
<!-- like that. This is the exactly place between sticky and normal posts -->
<!-- -->
<?php
$args = array('posts_per_page' => 12, 'post__not_in' => get_option('sticky_posts'));
$normalposts = new WP_Query($args);
while($normalposts->have_posts()) : $normalposts->the_post();
?>
<!-- normal posts without sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
這將在您的頁面頂部顯示 12 個置頂帖子。 之后,您可以輸入一些隨機文本,或 s.th。 像那樣,然后是 12 個“正常”帖子,這些帖子不是粘性帖子。
嘗試使用遞增變量(即$counter++
)以及循環內的條件語句,以在循環傳遞 all(3) 個粘性帖子后觸發您想要的內容。
<?php
$count=0; //Count is 0 outside of loop
while ( have_posts() ) : the_post(); // Begin WP loop
if(!is_sticky()) : $count++; endif; // Start count for nonsticky loops
if(!is_sticky() && $count==1){
// Do stuff when the first none-sticky post is discovered inside the loop
// ie. Add a div, or insert a class name, etc.
}
// Here is an example of an actual implementation
if(!is_sticky() && $count==1): echo '<div class="not-sticky">'; endif;
// Loop countinues
endwhile; // Loop ends
if(!is_sticky(): echo '</div><!-- .not-sticky -->'; // Placed outside of loop or add inside a conditional statement within the loop to only run once
?>
...多年后,但我希望它仍然可以幫助可能面臨同樣問題的其他人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.