0

我的代码在工作中不是最优的 - 它没有正确显示父类别帖子。他不是只显示父类别的帖子,而是抛出属于父类别的所有子类别的所有帖子。我对函数“in_category”有疑问 - 自定义帖子类型是否有任何替换?代码如下:

<?php

$categories = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => 0 , 'hide_empty'=> '0' ));

foreach ( $categories as $category ) {

if ( $category->parent > 0 ) {
continue;   
}

$i = 0;
echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';

$posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'CUSTOM_POST_TYPE' ) );  
echo '<ul>';
foreach ( $posts as $post ) {

$child_categories = get_term_children( $category->term_id, 'MY_CUSTOM_TAXONOMY' );

if ( $child_categories && in_category( $child_categories, $post->ID ) ) {
continue;
}
            setup_postdata($post);  

         echo '<li>'; the_title();   echo '</li>';   
}
echo '</ul>';

$categories2 = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => $category->term_id , 'hide_empty'=> '0' ));

foreach ( $categories2 as $category ) {

$j = 0;
echo '<h2>' . $category->name . '</h2>';


$posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'CUSTOM_POST_TYPE' ) );  

        echo '<ul>';
        foreach($posts as $post) :  
            setup_postdata($post);  

         echo '<li>'; the_title();   echo '</li>';

        endforeach;
        echo '</ul>';

}
}

?>
4

1 回答 1

0

这是我的工作解决方案:

<?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>';

}
}

?>
于 2013-01-18T10:37:18.460 回答