首页 文章

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

提问于
浏览
0

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

我收到所有信息,帖子按字母顺序排列,但 the terms are in order of id and need them in alphabetical order .

我正在捏造我的方式,目前正在使用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:
"; ?> <?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 "
"; } ?>

这是一个指向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

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

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

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

  • 0

    我的解决方案是安装插件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 (
    

相关问题