admin管理员组文章数量:1315831
I have a page template that shows all the "posts" within two custom taxonomies, the posts are displayed in a table
$type = $_GET['type'];
$category = $_GET['category'];
args = array(
'post-type' => 'literature',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $type
),
array(
'taxonomy' => 'custom-category',
'field' => 'slug',
'terms' => $category
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ( $query->have_posts() ) : $query->the_post();
The I pull some custom fields etc
Now I need the terms that are assigned to the post
<?php $terms = get_the_terms( $post, 'custom-category' ); foreach($terms as $term) {
?><td class="custom-cat"> <span class="term">
<?php echo $term->name; ?>
</span></td><?php
}
?>
Which works fine and on the front end I get
<td>Science</td> <td>Space</td>
<td>Earthquakes</td> <td>Science</td>
<td>Science</td> <td>Volcanoes</td>
<td>Science</td> <td>Space</td>
<td>Science</td> <td>Volcanoes</td>
<td>Earthquakes</td> <td>Science</td>
However, On the second and last result you can see it puts Earthquakes before Science?
My hierarchy is
Science (parent term)
-Earthquakes (child)
-Space (child)
-Volcanoes (child)
I am assuming this is because the default order is A-Z? If I could make this order by term_id then it should work?
How could I go about this please
I have a page template that shows all the "posts" within two custom taxonomies, the posts are displayed in a table
$type = $_GET['type'];
$category = $_GET['category'];
args = array(
'post-type' => 'literature',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $type
),
array(
'taxonomy' => 'custom-category',
'field' => 'slug',
'terms' => $category
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ( $query->have_posts() ) : $query->the_post();
The I pull some custom fields etc
Now I need the terms that are assigned to the post
<?php $terms = get_the_terms( $post, 'custom-category' ); foreach($terms as $term) {
?><td class="custom-cat"> <span class="term">
<?php echo $term->name; ?>
</span></td><?php
}
?>
Which works fine and on the front end I get
<td>Science</td> <td>Space</td>
<td>Earthquakes</td> <td>Science</td>
<td>Science</td> <td>Volcanoes</td>
<td>Science</td> <td>Space</td>
<td>Science</td> <td>Volcanoes</td>
<td>Earthquakes</td> <td>Science</td>
However, On the second and last result you can see it puts Earthquakes before Science?
My hierarchy is
Science (parent term)
-Earthquakes (child)
-Space (child)
-Volcanoes (child)
I am assuming this is because the default order is A-Z? If I could make this order by term_id then it should work?
How could I go about this please
Share Improve this question asked Nov 17, 2020 at 10:50 user1348927user1348927 111 silver badge6 bronze badges 3 |1 Answer
Reset to default 0I just realised
I already have the parent from here $category = $_GET['category'];
So I changed my code:
<td class="custom-cat"> <span class="term">
<?php echo $category; ?>
</span></td>
<td class="custom-sub-cat"> <span class="term">
<?php
$terms = get_the_terms( get_the_ID(), 'custom-category' );
foreach ( $terms as $term ){
if ( $term->parent ==! 0 ) {
echo $term->name;
}
}
?>
</span></td>
As I only have one parent and one child for each taxonomy the above works fine for me as a work around.
本文标签: categoriesOrdering terms whilst in loop
版权声明:本文标题:categories - Ordering terms whilst in loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741988132a2408805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<?php echo $term->name . '- ' . $term->parent; ?>
– Tom J Nowell ♦ Commented Nov 17, 2020 at 11:29