admin管理员组文章数量:1122846
I have a custom post type and a taxonomy with children, with a template file named taxonomy-productcategories.php. On this i want to list all children with their posts.
Child taxonomy 1
- Child taxonomy 1 Post A
- Child taxonomy 1 Post B
- Child taxonomy 1 Post C
Child taxonomy 2
- Child taxonomy 2 Post D
- Child taxonomy 2 Post E
- Child taxonomy 2 Post F
And so on...
I have an example for getting just the Child taxonomy, but i want to add the post from each taxonomy.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo "$term->name:<br />";
}
?>
I have a custom post type and a taxonomy with children, with a template file named taxonomy-productcategories.php. On this i want to list all children with their posts.
Child taxonomy 1
- Child taxonomy 1 Post A
- Child taxonomy 1 Post B
- Child taxonomy 1 Post C
Child taxonomy 2
- Child taxonomy 2 Post D
- Child taxonomy 2 Post E
- Child taxonomy 2 Post F
And so on...
I have an example for getting just the Child taxonomy, but i want to add the post from each taxonomy.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo "$term->name:<br />";
}
?>
Share
Improve this question
edited Nov 12, 2013 at 19:36
Maruti Mohanty
2,4041 gold badge19 silver badges21 bronze badges
asked Nov 12, 2013 at 17:09
GustavGustav
214 bronze badges
2
- The solution I would go with is probably using WP_Query() but that might be inefficient / slow but it would get the job done. – Howdy_McGee ♦ Commented Nov 12, 2013 at 20:59
- Yes, that was my thought too, som i looked fore that type of solution, but the only thing i found was geting it done with a specific post type and taxomony. – Gustav Commented Nov 13, 2013 at 7:55
1 Answer
Reset to default 0I had an idea of combining the first code with this solution that lists posts from a specifik taxomony:
<?php $terms = get_terms('productcategories');
foreach ($terms as $term) {
$wpq = array (
'taxonomy'=>'productcategories',
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
I managed to combine the two, so this is my current solution. Maybe not the best but it works.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
本文标签: termsList post from current taxonomy children
版权声明:本文标题:terms - List post from current taxonomy children 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282563a1926681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论