admin管理员组文章数量:1130349
I am a bit lost and looking for guidance. I have a homepage that lists blog posts. However after every 3rd post (we'll call this the "Main Feed") I want to add a section in between that looks different and calls a specific category. After that section I want the "Main Feed" to continue.
I've only ever had to call one section at a time and really looking for some guidance. I appreciate the help greatly!
Main Feed
<?php query_posts('cat=-20'); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=3&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=4&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like this repeating pattern where specific categories are inserted throughout the main feed every 3 posts.
I am a bit lost and looking for guidance. I have a homepage that lists blog posts. However after every 3rd post (we'll call this the "Main Feed") I want to add a section in between that looks different and calls a specific category. After that section I want the "Main Feed" to continue.
I've only ever had to call one section at a time and really looking for some guidance. I appreciate the help greatly!
Main Feed
<?php query_posts('cat=-20'); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=3&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=4&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like this repeating pattern where specific categories are inserted throughout the main feed every 3 posts.
Share Improve this question edited Dec 17, 2018 at 17:04 user5854648 asked Dec 17, 2018 at 16:51 user5854648user5854648 16317 bronze badges 4 |1 Answer
Reset to default 0I've found old pieces of code and joined it with yours. It could be more elegant, but you have the idea.
<?php
$args = array(
'category__not_in' => array( 20 ),
);
$query = new WP_Query( $args );
$posts = $query->posts;
wp_reset_postdata();
$post_counter = 1;
// secondary posts' categories (ids)
$sec_categories = [2, 14, 56, 234];
$cat_counter = 0;
foreach( $posts as $post ) {
echo '<h1>' . $post->post_title . '</h1>';
echo '<p>' . $post->post_excerpt . '</p>';
// after each 3rd post goes (two) secondary
if( $post_counter % 3 == 0 ) {
query_posts( 'cat=' . $sec_categories[$cat_counter] . '&posts_per_page=2' );
while ( have_posts() ) {
the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . get_the_excerpt() . '</p>';
}
wp_reset_query();
$cat_counter++;
}
$post_counter++;
}
You have to think out how to choose categories for secondary posts
I am a bit lost and looking for guidance. I have a homepage that lists blog posts. However after every 3rd post (we'll call this the "Main Feed") I want to add a section in between that looks different and calls a specific category. After that section I want the "Main Feed" to continue.
I've only ever had to call one section at a time and really looking for some guidance. I appreciate the help greatly!
Main Feed
<?php query_posts('cat=-20'); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=3&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=4&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like this repeating pattern where specific categories are inserted throughout the main feed every 3 posts.
I am a bit lost and looking for guidance. I have a homepage that lists blog posts. However after every 3rd post (we'll call this the "Main Feed") I want to add a section in between that looks different and calls a specific category. After that section I want the "Main Feed" to continue.
I've only ever had to call one section at a time and really looking for some guidance. I appreciate the help greatly!
Main Feed
<?php query_posts('cat=-20'); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=3&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This Should Interrupt Main Feed Once Every 3 Posts
<?php query_posts(''cat=4&posts_per_page=2''); while (have_posts()) : the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like this repeating pattern where specific categories are inserted throughout the main feed every 3 posts.
Share Improve this question edited Dec 17, 2018 at 17:04 user5854648 asked Dec 17, 2018 at 16:51 user5854648user5854648 16317 bronze badges 4- show the code that displays your posts on the homepage – RiddleMeThis Commented Dec 17, 2018 at 16:56
- Sure thing, I've gone ahead and added the code with additional explanation, hope that helps. Thanks! – user5854648 Commented Dec 17, 2018 at 17:04
-
Related: It's not a good idea to use
query_posts(). – Pat J Commented Dec 17, 2018 at 19:20 -
...Though on second reading, I note you're using
wp_reset_query(), which is good practice if you insist on usingquery_posts(). – Pat J Commented Dec 17, 2018 at 19:28
1 Answer
Reset to default 0I've found old pieces of code and joined it with yours. It could be more elegant, but you have the idea.
<?php
$args = array(
'category__not_in' => array( 20 ),
);
$query = new WP_Query( $args );
$posts = $query->posts;
wp_reset_postdata();
$post_counter = 1;
// secondary posts' categories (ids)
$sec_categories = [2, 14, 56, 234];
$cat_counter = 0;
foreach( $posts as $post ) {
echo '<h1>' . $post->post_title . '</h1>';
echo '<p>' . $post->post_excerpt . '</p>';
// after each 3rd post goes (two) secondary
if( $post_counter % 3 == 0 ) {
query_posts( 'cat=' . $sec_categories[$cat_counter] . '&posts_per_page=2' );
while ( have_posts() ) {
the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . get_the_excerpt() . '</p>';
}
wp_reset_query();
$cat_counter++;
}
$post_counter++;
}
You have to think out how to choose categories for secondary posts
本文标签: Insert Content In Between Post Feed
版权声明:本文标题:Insert Content In Between Post Feed 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749090369a2314422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


query_posts(). – Pat J Commented Dec 17, 2018 at 19:20wp_reset_query(), which is good practice if you insist on usingquery_posts(). – Pat J Commented Dec 17, 2018 at 19:28