【问题标题】:How to get a list of taxonomy child terms in alphabetical order with posts in Wordpress如何在 Wordpress 中按字母顺序获取分类子术语列表以及帖子
【发布时间】: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


    【解决方案1】:

    用您上面提供的代码替换此代码

    <?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 />";   
    }
    ?>
    

    让我知道这是否适合你...

    【讨论】:

    • 嗨,这在我的情况下不起作用。这会导致不显示任何子术语,并且列出所有帖子,而不仅仅是父子术语中的帖子。
    • 用你的代码替换上面的代码希望对你有用
    • 不,抱歉,它不起作用。我没有子术语,并且再次列出所有帖子,无论它们在哪个术语中。 ksort 提出了一个子术语,它以 A 开头,所以它按字母顺序出现,但没有显示其他术语或帖子。还有其他想法吗?我在我的问题中添加了指向完整分类模板代码的链接。
    • 我已编辑代码替换该代码,如果您有运气的话
    • Laraib,感谢您抽出宝贵时间,在看到您的回答之前,我安装了 Custom Taxonomy Order NE 插件,作者调整了我的代码,现在它可以工作了。我会发布一个答案。
    【解决方案2】:

    在我的情况下,解决方案是安装插件 Custom Taxonomy Order 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 (
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-17
      • 2013-10-10
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      相关资源
      最近更新 更多