首页 > 解决方案 > 如何按类别对自定义分类循环进行排序

问题描述

我创建了一个自定义分类法,它有各种不同的术语。基本上该月的所有帖子都分配给特定的术语,但此外所有帖子还分配有标准类别。

为自定义和税收创建模板并使用 WP_Query 我设法显示属于特定自定义税收的所有帖子。现在,我还希望按帖子类别对自定义税收循环中的所有帖子进行排序(例如,按经济、政治、技术等对帖子进行分组)。我曾尝试使用嵌套循环,但没有奏效。

对 php 来说非常新,任何帮助都非常有用。

<?php

$al_cat_slug = get_queried_object()->slug;
$al_cat_name = get_queried_object()->name;
?>
    <h2><?php echo $al_cat_name; ?></h2>

<?php 
    $al_tax_post_args = array(
        'post_type' => 'post', 
        'orderby' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'ausgabe',
                'field' => 'slug',
                'terms' => $al_cat_slug,
            )
        )
    );

$query = new WP_Query($al_tax_post_args);

if ( $query->have_posts() ) { ?>

    <section>
	    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

	    <article id="post-<?php the_ID(); ?>" class="fonds-article">

            <h3 class="fonds-main-title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>

        </article>

        <?php endwhile; ?>
 
   </section>
   <?php } ?>

标签: phpwordpress

解决方案


推荐阅读