我需要你的帮助。我想将粘性帖子与主循环(tempate: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.
提前致谢。
问候– Pomy
#1
在索引中进行标准循环之前, 你要在创建区域的地方创建要加载”粘性”帖子的位置
在帖子页面上写
<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条帖子
#2
尝试这个:
<?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个”普通”帖子, 而不是粘性帖子。
#3
尝试在循环内使用递增变量(即$ 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
?>
…几年后, 但我希望它仍然可以帮助可能面临相同问题的其他人。
评论前必须登录!
注册