admin管理员组文章数量:1130349
I'm using the following code to attempt to loop through each of the terms within my custom taxonomy category-film and then output the term name as a heading together with displaying all the custom posts that belong to that term
<?php $terms = get_terms( 'category-film' ); ?>
<?php foreach( $terms as $term ) : ?>
<?php $posts = new WP_Query( 'post=film&category-film= ' . $term->slug . '&posts_per_page=-1' ); ?>
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h3><?php echo $term->name; ?></h3>
<p><?php the_title(); ?></p>
<div class="col-md-6">
<?php the_field('url'); ?>
</div>
<?php endwhile; endif; ?>
<?php endforeach; ?>
I am finding, however, that my code outputs nothing whatsoever.
If I move <h3><?php echo $term->name; ?></h3> so that it is outside of the loop (but within the foreach loop) it outputs the names of the terms fine.
So I imagine the problem must be something to do with the loop itself?
I'm using the following code to attempt to loop through each of the terms within my custom taxonomy category-film and then output the term name as a heading together with displaying all the custom posts that belong to that term
<?php $terms = get_terms( 'category-film' ); ?>
<?php foreach( $terms as $term ) : ?>
<?php $posts = new WP_Query( 'post=film&category-film= ' . $term->slug . '&posts_per_page=-1' ); ?>
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h3><?php echo $term->name; ?></h3>
<p><?php the_title(); ?></p>
<div class="col-md-6">
<?php the_field('url'); ?>
</div>
<?php endwhile; endif; ?>
<?php endforeach; ?>
I am finding, however, that my code outputs nothing whatsoever.
If I move <h3><?php echo $term->name; ?></h3> so that it is outside of the loop (but within the foreach loop) it outputs the names of the terms fine.
So I imagine the problem must be something to do with the loop itself?
Share Improve this question asked Oct 5, 2015 at 17:56 user39214user39214 6 | Show 1 more comment1 Answer
Reset to default 10If your things are laid properly, this code will output 10 posts from film CPT, where taxonomy is category-film and it will occur each of the term of that particular taxonomy. I'm not aware about your templating, so lay your layout accordingly.
<?php
$_terms = get_terms( array('category-film') );
foreach ($_terms as $term) :
$term_slug = $term->slug;
$_posts = new WP_Query( array(
'post_type' => 'film',
'posts_per_page' => 10, //important for a PHP memory limit warning
'tax_query' => array(
array(
'taxonomy' => 'category-film',
'field' => 'slug',
'terms' => $term_slug,
),
),
));
if( $_posts->have_posts() ) :
echo '<h3>'. $term->name .'</h3>';
echo '<div class="row">';
while ( $_posts->have_posts() ) : $_posts->the_post();
?>
<div class="col-sm-6">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta( get_the_ID(), 'url', true ); ?></p>
</div>
<?php
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
endforeach;
?>
I'm using the following code to attempt to loop through each of the terms within my custom taxonomy category-film and then output the term name as a heading together with displaying all the custom posts that belong to that term
<?php $terms = get_terms( 'category-film' ); ?>
<?php foreach( $terms as $term ) : ?>
<?php $posts = new WP_Query( 'post=film&category-film= ' . $term->slug . '&posts_per_page=-1' ); ?>
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h3><?php echo $term->name; ?></h3>
<p><?php the_title(); ?></p>
<div class="col-md-6">
<?php the_field('url'); ?>
</div>
<?php endwhile; endif; ?>
<?php endforeach; ?>
I am finding, however, that my code outputs nothing whatsoever.
If I move <h3><?php echo $term->name; ?></h3> so that it is outside of the loop (but within the foreach loop) it outputs the names of the terms fine.
So I imagine the problem must be something to do with the loop itself?
I'm using the following code to attempt to loop through each of the terms within my custom taxonomy category-film and then output the term name as a heading together with displaying all the custom posts that belong to that term
<?php $terms = get_terms( 'category-film' ); ?>
<?php foreach( $terms as $term ) : ?>
<?php $posts = new WP_Query( 'post=film&category-film= ' . $term->slug . '&posts_per_page=-1' ); ?>
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h3><?php echo $term->name; ?></h3>
<p><?php the_title(); ?></p>
<div class="col-md-6">
<?php the_field('url'); ?>
</div>
<?php endwhile; endif; ?>
<?php endforeach; ?>
I am finding, however, that my code outputs nothing whatsoever.
If I move <h3><?php echo $term->name; ?></h3> so that it is outside of the loop (but within the foreach loop) it outputs the names of the terms fine.
So I imagine the problem must be something to do with the loop itself?
Share Improve this question asked Oct 5, 2015 at 17:56 user39214user39214 6-
Do not use
$postsas a custom variable, it breaks and stuffs up the$postsglobal, rather use something like$posts_array– Pieter Goosen Commented Oct 5, 2015 at 17:58 - 1 Also, just a note, your query is very expensive :-) – Pieter Goosen Commented Oct 5, 2015 at 17:59
- thanks @PieterGoosen, I've tried that but unfortunately still having the same issue. I can't seem to output anything in the loop, even if just static content – user39214 Commented Oct 5, 2015 at 18:03
- You can try something like this, just note, this was done for the main query, so you will need to adjust it slightly for a custom query – Pieter Goosen Commented Oct 5, 2015 at 18:03
-
1
Use a proper
tax_query– Pieter Goosen Commented Oct 5, 2015 at 18:04
1 Answer
Reset to default 10If your things are laid properly, this code will output 10 posts from film CPT, where taxonomy is category-film and it will occur each of the term of that particular taxonomy. I'm not aware about your templating, so lay your layout accordingly.
<?php
$_terms = get_terms( array('category-film') );
foreach ($_terms as $term) :
$term_slug = $term->slug;
$_posts = new WP_Query( array(
'post_type' => 'film',
'posts_per_page' => 10, //important for a PHP memory limit warning
'tax_query' => array(
array(
'taxonomy' => 'category-film',
'field' => 'slug',
'terms' => $term_slug,
),
),
));
if( $_posts->have_posts() ) :
echo '<h3>'. $term->name .'</h3>';
echo '<div class="row">';
while ( $_posts->have_posts() ) : $_posts->the_post();
?>
<div class="col-sm-6">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta( get_the_ID(), 'url', true ); ?></p>
</div>
<?php
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
endforeach;
?>
本文标签: loopLooping Through Custom Tax Terms and Displaying All Posts For Each
版权声明:本文标题:loop - Looping Through Custom Tax Terms and Displaying All Posts For Each 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749082282a2313234.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$postsas a custom variable, it breaks and stuffs up the$postsglobal, rather use something like$posts_array– Pieter Goosen Commented Oct 5, 2015 at 17:58tax_query– Pieter Goosen Commented Oct 5, 2015 at 18:04