admin管理员组文章数量:1026989
I have a post type that includes a taxonomy for (1) location and (2) a day of the week.
I'm using the first taxonomy to sort the posts into groups. Here's the loop I'm using:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'orderby' => 'by_term',
'hide_empty' => false
);
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo '<div class="accordionButton">';
echo "<h2 class=\"cellHeader\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h2>";
echo '</div>';
echo '<div class="accordionContent">';
if ($article_count) {
echo "<ul class='cell_list'>";
while ($myquery->have_posts()) : $myquery->the_post();?>
<li class="cell-item">
<ul class="cell-list">
<li><?php $terms_as_text = get_the_term_list( $post->ID, 'cell-days', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> </li>
<li> <? echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true);?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true);?></a></li>
</ul>
</li>
<?php endwhile;
echo "</ul>";
}
echo '</div>';
}
?>
This gives me a nice accordion-style layout based on the taxonomy term, "cells-location." This works all fine, except I now want to sort the posts within each location according to the other taxonomy, "cells-days." I've used a plugin to give them a sort order (/). The api for the plugin offers the following query arguments to call the posts in order:
$argv = array(
'orderby' => 'term_order',
'hide_empty' => false
);
get_terms('category', $argv);
I'm having trouble creating this second loop within the first loop. Any thoughts or suggestions?
I have a post type that includes a taxonomy for (1) location and (2) a day of the week.
I'm using the first taxonomy to sort the posts into groups. Here's the loop I'm using:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'orderby' => 'by_term',
'hide_empty' => false
);
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo '<div class="accordionButton">';
echo "<h2 class=\"cellHeader\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h2>";
echo '</div>';
echo '<div class="accordionContent">';
if ($article_count) {
echo "<ul class='cell_list'>";
while ($myquery->have_posts()) : $myquery->the_post();?>
<li class="cell-item">
<ul class="cell-list">
<li><?php $terms_as_text = get_the_term_list( $post->ID, 'cell-days', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> </li>
<li> <? echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true);?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true);?></a></li>
</ul>
</li>
<?php endwhile;
echo "</ul>";
}
echo '</div>';
}
?>
This gives me a nice accordion-style layout based on the taxonomy term, "cells-location." This works all fine, except I now want to sort the posts within each location according to the other taxonomy, "cells-days." I've used a plugin to give them a sort order (http://wordpress/extend/plugins/taxonomy-terms-order/). The api for the plugin offers the following query arguments to call the posts in order:
$argv = array(
'orderby' => 'term_order',
'hide_empty' => false
);
get_terms('category', $argv);
I'm having trouble creating this second loop within the first loop. Any thoughts or suggestions?
Share Improve this question asked Oct 27, 2012 at 21:55 jsumnersmithjsumnersmith 612 bronze badges1 Answer
Reset to default 1Part of your trouble likely stems from the fact that you're using the legacy version of the get_terms()
function. As per the docs for this function:
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array( 'taxonomy' => 'post_tag', 'hide_empty' => false, ) );
Therefore, we can rewrite your get_terms()
and $argv
for the cells-days
taxonomy like so:
$argv = array(
'taxonomy' => 'cells-days',
'orderby' => 'by_term',
'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );
Then you can create another foreach
loop inside of your first <li>
element in ul.cell-list
(At least that seems to be where you're attempting to loop through cells-days
):
<ul class="cell-list">
<li>
<?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
<span class="cells-days"><?= ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ) ?></span>
<?php endforeach; ?>
</li>
<!-- The other <li> elements here -->
</ul>
On a separate note, I recommend reworking the first part to use HTML where you're echoing, so when all is said and done your code would look like this:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'taxonomy' => 'cells-days',
'orderby' => 'by_term',
'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );
foreach ($terms as $term):
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count; ?>
<div class="accordionButton">
<h2 class="cellHeader" id="<?= $term->slug ?>">
<?= $term->name ?>
</h2>
</div>
<div class="accordionContent">
<?php if ($article_count): ?>
<ul class="cell_list">
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li class="cell-item">
<ul class="cell-list">
<li>
<?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
<span class="cells-days"><?php echo ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ); ?></span>
<?php endforeach; ?>
</li>
<li><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true); ?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?></a></li>
</ul>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
I have a post type that includes a taxonomy for (1) location and (2) a day of the week.
I'm using the first taxonomy to sort the posts into groups. Here's the loop I'm using:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'orderby' => 'by_term',
'hide_empty' => false
);
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo '<div class="accordionButton">';
echo "<h2 class=\"cellHeader\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h2>";
echo '</div>';
echo '<div class="accordionContent">';
if ($article_count) {
echo "<ul class='cell_list'>";
while ($myquery->have_posts()) : $myquery->the_post();?>
<li class="cell-item">
<ul class="cell-list">
<li><?php $terms_as_text = get_the_term_list( $post->ID, 'cell-days', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> </li>
<li> <? echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true);?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true);?></a></li>
</ul>
</li>
<?php endwhile;
echo "</ul>";
}
echo '</div>';
}
?>
This gives me a nice accordion-style layout based on the taxonomy term, "cells-location." This works all fine, except I now want to sort the posts within each location according to the other taxonomy, "cells-days." I've used a plugin to give them a sort order (/). The api for the plugin offers the following query arguments to call the posts in order:
$argv = array(
'orderby' => 'term_order',
'hide_empty' => false
);
get_terms('category', $argv);
I'm having trouble creating this second loop within the first loop. Any thoughts or suggestions?
I have a post type that includes a taxonomy for (1) location and (2) a day of the week.
I'm using the first taxonomy to sort the posts into groups. Here's the loop I'm using:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'orderby' => 'by_term',
'hide_empty' => false
);
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo '<div class="accordionButton">';
echo "<h2 class=\"cellHeader\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h2>";
echo '</div>';
echo '<div class="accordionContent">';
if ($article_count) {
echo "<ul class='cell_list'>";
while ($myquery->have_posts()) : $myquery->the_post();?>
<li class="cell-item">
<ul class="cell-list">
<li><?php $terms_as_text = get_the_term_list( $post->ID, 'cell-days', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> </li>
<li> <? echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true);?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true);?></a></li>
</ul>
</li>
<?php endwhile;
echo "</ul>";
}
echo '</div>';
}
?>
This gives me a nice accordion-style layout based on the taxonomy term, "cells-location." This works all fine, except I now want to sort the posts within each location according to the other taxonomy, "cells-days." I've used a plugin to give them a sort order (http://wordpress/extend/plugins/taxonomy-terms-order/). The api for the plugin offers the following query arguments to call the posts in order:
$argv = array(
'orderby' => 'term_order',
'hide_empty' => false
);
get_terms('category', $argv);
I'm having trouble creating this second loop within the first loop. Any thoughts or suggestions?
Share Improve this question asked Oct 27, 2012 at 21:55 jsumnersmithjsumnersmith 612 bronze badges1 Answer
Reset to default 1Part of your trouble likely stems from the fact that you're using the legacy version of the get_terms()
function. As per the docs for this function:
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array( 'taxonomy' => 'post_tag', 'hide_empty' => false, ) );
Therefore, we can rewrite your get_terms()
and $argv
for the cells-days
taxonomy like so:
$argv = array(
'taxonomy' => 'cells-days',
'orderby' => 'by_term',
'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );
Then you can create another foreach
loop inside of your first <li>
element in ul.cell-list
(At least that seems to be where you're attempting to loop through cells-days
):
<ul class="cell-list">
<li>
<?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
<span class="cells-days"><?= ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ) ?></span>
<?php endforeach; ?>
</li>
<!-- The other <li> elements here -->
</ul>
On a separate note, I recommend reworking the first part to use HTML where you're echoing, so when all is said and done your code would look like this:
<?php
$terms = get_terms('cell-locations');
$argv = array(
'taxonomy' => 'cells-days',
'orderby' => 'by_term',
'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );
foreach ($terms as $term):
$wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count; ?>
<div class="accordionButton">
<h2 class="cellHeader" id="<?= $term->slug ?>">
<?= $term->name ?>
</h2>
</div>
<div class="accordionContent">
<?php if ($article_count): ?>
<ul class="cell_list">
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li class="cell-item">
<ul class="cell-list">
<li>
<?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
<span class="cells-days"><?php echo ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ); ?></span>
<?php endforeach; ?>
</li>
<li><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
<li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), '_cell_leader_email', true); ?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?></a></li>
</ul>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
本文标签: multi taxonomy queryUsing multiple taxonomies to sort Custom Posts
版权声明:本文标题:multi taxonomy query - Using multiple taxonomies to sort Custom Posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659223a2161787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论