Wordpress 包含子项和帖子的自定义分类列表,不起作用

Wordpress 包含子项和帖子的自定义分类列表,不起作用,wordpress,taxonomy,Wordpress,Taxonomy,我有这段代码可以正常工作,但问题在于显示数据的顺序。现在,它是一个小混乱和职位是重复的,当你检查子类别的位置。如何显示类别树,每个类别和子类别中都包含帖子,例如: <h1>Prime Category 1</h1> <ul> <li>Post 1</li> <li>Post 2</li> <li>...</li> </u

我有这段代码可以正常工作,但问题在于显示数据的顺序。现在,它是一个小混乱和职位是重复的,当你检查子类别的位置。如何显示类别树,每个类别和子类别中都包含帖子,例如:

<h1>Prime Category 1</h1>
    <ul>
        <li>Post 1</li>
        <li>Post 2</li>
        <li>...</li>
    </ul>
<h2>Sub Category 1</h2> 
    <ul>
        <li>Post 1</li>
        <li> Post 2</li>
        <li>...</li>
    </ul>
<h2>Sub Category 2</h2> 
    <ul>
        <li>Post 1</li>
        <li> ...</li>
    </ul>
<h1>Prime Category 2</h1>
<h2>Sub Category</h2> 
    <ul>
        <li>Post 1</li>
        <li> ...</li>
    </ul>
<h2>Sub Category</h2> 
    <ul>
        <li>Post 1</li>
        <li> ...</li>
    </ul>  
Prime类别1
  • 职位1
  • 职位2
第1小类
  • 职位1
  • 职位2
次级类别2
  • 职位1
素类2 子类别
  • 职位1
子类别
  • 职位1
这是我的密码

<?php
    $post_type = 'biblioteka';
    $tax = 'kategoria-pozycji';
    $tax_terms = get_terms( $tax );
    if ($tax_terms) {
        foreach ($tax_terms  as $tax_term) {

        $args = array(

            'post_type' => $post_type,
            'child_of' => $tax_term->term_id,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
                'posts_per_page' => 2,
            'hierarchical' => true,
            'caller_get_posts'=> 1);

        $my_query = null;
        $my_query = new WP_Query($args);


     if( $my_query->have_posts() ) : ?>


        <h1><?php echo $tax_term->name; ?></h1>

        <ul>
        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

            <li id="post-<?php the_ID(); ?>">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>


        <?php endwhile; ?>

        </ul>

    <?php else : ?>
    <?php endif; wp_reset_query();
            }
        }
    ?>


    谢谢大家的帮助。。。这是一个有效的解决方案:

    <?php
    
    $args=array(
    'post_type'                => 'biblioteka',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'kategoria-pozycji',
    'pad_counts'               => false
    );
    
    $categories=get_categories($args);
    
    foreach ( $categories as $category ) {
    
    if ( $category->parent > 0 ) {
        continue;   
    }
    
    echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
    
        $querystr = "SELECT $wpdb->posts.*
                      FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
                      WHERE term_id = (" . $category->cat_ID . ")
                      AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
                      AND ID = object_id
                      AND post_type = 'biblioteka'
                      AND post_status = 'publish'
                      ORDER BY post_date DESC";
        $posts = $wpdb->get_results($querystr, OBJECT);
    
        echo '<ul>';
            foreach ( $posts as $post ) {
                setup_postdata($post);  
    
                    echo '<li>'; the_title();   echo '</li>';
    
                    }
        echo '</ul>';
    
    $categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
    
    foreach ( $categories2 as $category ) {
    
    echo '<h2>' . $category->name . '</h2>';
    
    $posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );  
    
        echo '<ul>';
            foreach($posts as $post) { 
                setup_postdata($post);  
    
                    echo '<li>'; the_title();   echo '</li>';
    
                    }
        echo '</ul>';
    
    }
    }
    
    ?>
    
    
    
    谢谢大家的帮助。。。这是一个有效的解决方案:

    <?php
    
    $args=array(
    'post_type'                => 'biblioteka',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'kategoria-pozycji',
    'pad_counts'               => false
    );
    
    $categories=get_categories($args);
    
    foreach ( $categories as $category ) {
    
    if ( $category->parent > 0 ) {
        continue;   
    }
    
    echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
    
        $querystr = "SELECT $wpdb->posts.*
                      FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
                      WHERE term_id = (" . $category->cat_ID . ")
                      AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
                      AND ID = object_id
                      AND post_type = 'biblioteka'
                      AND post_status = 'publish'
                      ORDER BY post_date DESC";
        $posts = $wpdb->get_results($querystr, OBJECT);
    
        echo '<ul>';
            foreach ( $posts as $post ) {
                setup_postdata($post);  
    
                    echo '<li>'; the_title();   echo '</li>';
    
                    }
        echo '</ul>';
    
    $categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
    
    foreach ( $categories2 as $category ) {
    
    echo '<h2>' . $category->name . '</h2>';
    
    $posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );  
    
        echo '<ul>';
            foreach($posts as $post) { 
                setup_postdata($post);  
    
                    echo '<li>'; the_title();   echo '</li>';
    
                    }
        echo '</ul>';
    
    }
    }
    
    ?>
    
    
    
    尝试使用表格而不是列表。这不会有帮助。问题在于对数据库的错误查询。这是一个查询的结果。结果没有显示与类别一致的排列-请看图片尝试使用表格而不是列表。这不会有帮助。问题在于对数据库的错误查询。这是一个查询的结果。结果未显示与类别一致的排列-参见图片