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 に答える