【发布时间】:2016-08-22 20:57:36
【问题描述】:
我正在努力让一个非常简单的 Wordpress 自定义分类归档模板能够正常工作。
我正在获取所有信息,帖子按字母顺序列出,但术语按 id 顺序排列,需要按字母顺序排列。
我正在搞砸这个,目前正在使用来自this post 的代码。我从网上尝试了多种解决方案,但没有运气。我看到this post has a solution,但不知道如何在下面的代码中实现它。
也许有一种更简单的方法可以满足我的需要?
查询需要获取当前父词条,然后是子词条,以及子词条中的帖子。以下代码在我的 taxonomy-business-categories-(parent term).php 上,例如我的 taxonomy-business-categories-bars.php。我需要输出与其帖子分组的子术语。所有必须按字母顺序排列。
<?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 />";
}
?>
这是指向 taxonomy template 的 .txt 链接。文件。
更新:由于我正在按父术语对分类模板进行硬编码,我可以在上面的代码中使用类似的东西吗:
<?php
$term_id = 32;
$taxonomy_name = 'business-categories';
$termchildren = get_term_children( $term_id, $taxonomy_name );
$children = array();
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$children[$term->name] = $term;
}
ksort($children);
【问题讨论】:
标签: wordpress foreach custom-taxonomy