如何使用Wordpress中的帖子按字母顺序获取分类法子项列表

时间:2016-04-28 08:09:30

标签: wordpress foreach custom-taxonomy

我正在努力获得一个非常简单的Wordpress自定义分类法归档模板。

我获取了所有信息,帖子按字母顺序列出,但这些字词按照ID的顺序排列,需要按字母顺序排列

我勉强通过这种方式,目前正在使用this post中的代码。我尝试了很多来自网络的解决方案,没有运气。我看到this post has a solution但不知道如何在下面的代码中实现它。

也许有更简单的方法来做我需要的事情?

查询需要获取当前的父条款,然后是子条款以及子条款中的帖子。以下代码在我的分类 - 业务类别 - (父条款).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);

2 个答案:

答案 0 :(得分:0)

将此代码替换为您提供的上述代码

<?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( 'name', $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 />";   
}
?>

如果这对您有用,请告诉我......

答案 1 :(得分:0)

在我的案例中,解决方案是安装插件自定义分类法订单NE并(根据插件的作者的指示)调整查询,如下所示:

替换它:

$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (

有了这个

$termchildren = get_terms( array(
'taxonomy' => $taxonomyName,
'child_of' => $current_term->term_id,

));

foreach ($termchildren as $term) {
$wpq = array (