web-dev-qa-db-fra.com

Liste avec catégories, sous-catégories et publications de type de publication personnalisée

J'essaie d'interroger une liste avec toutes les catégories, sous-catégories et publications d'un type de publication personnalisé.

par exemple.:

Maincat1
  -Subcat1
     -Post1
     -Post2
  -Subcat2
  -Subcat3
Maincat2
  -Subcat3
     -Post3
Maincat3
  -Post4

Je commence à m'énerver en essayant d'y parvenir. J'ai essayé tellement de choses que j'ai trouvées sur le Web, mais je n'arrive pas à comprendre comment le faire fonctionner.

Ce que j’ai jusqu’à présent (au travail), Toutes les catégories + les sous-catégories, mais je ne comprends pas comment interroger les publications à partir des catégories/sous-catégories.

<?php

$orderby      = 'name';
$show_count   = 1;      // 1 for yes, 0 for no
$pad_counts   = 1;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';
$empty        = 0;

$args = array(
  'taxonomy'     => 'kategorie',
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);

?>
<?php

$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('kategorie',$parent_cat_arg);//category name

foreach ($parent_cat as $catVal) {

    echo '<h3>'.$catVal->name.'</h3>'; //Parent Category

$args = array('post_type' => 'Dokumente',
        'tax_query' => array(
            array(
                'taxonomy' => 'kategorie',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );


$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php
    endwhile;
}

wp_reset_query();


    $child_arg = array( 'hide_empty' => false, 'parent' => $catVal->term_id );
    $child_cat = get_terms( 'kategorie', $child_arg );

    echo '<ul>';
        foreach( $child_cat as $child_term ) { 
            echo '<li>'.$child_term->name . '</li>'; //Child Category



$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php            
    endwhile;
}

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

}
?>
</div>
<?php

}

add_shortcode('kategorien', 'get_mylist' );

J'ai aussi le code pour interroger les articles (du moins, je pense que cela fonctionne), mais je ne sais pas comment les combiner.

$custom_terms = get_terms('kategorie');

foreach($custom_terms as $custom_term) {
wp_reset_query();

$args_slugs = array('post_type' => 'Dokumente',
        'tax_query' => array(
            array(
                'taxonomy' => 'kategorie',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );


     $loop = new WP_Query($args_slugs);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo '<ul><li><a href="'.get_permalink().'">'.get_the_title().'</a></li></ul><br>';
        endwhile;
     }
}

J'espère vraiment que quelqu'un pourra m'aider ... Et oui, j'avais déjà utilisé la recherche et trouvé des questions similaires mais aucune des réponses ne fonctionnait pour moi.

Meilleur, NixXxon

2
Niko

@niko S'il vous plaît, trouvez votre code requis, il vous donnera le résultat exact que vous cherchiez. Je pense que vous alliez bien sauf que dans la sortie finale, il vous manquait la bonne structure.

Aussi, je recommande de ne pas assigner de publications à la catégorie parente car cette structure ne supporte pas cela. Cependant, je pense qu'après quelques ajustements mineurs, cela peut également être réalisé.

<?php   
    $taxonomy = 'testimonial-category';
    $postType = 'testimonial';
    $terms = get_terms(['taxonomy' => $taxonomy, 'orderby' => 'term_id', 'parent' => 0, 'hide_empty' => false]);
?>
<div class="">
<?php
    foreach($terms as $term){
        echo '<h3>' . $term->name . '</h3>';
        $childTerms = get_terms(['taxonomy' => $taxonomy, 'orderby' => 'term_id', 'parent' => $term->term_id, 'hide_empty' => false]);

        foreach($childTerms as $childTerm)
        {
            $posts = get_posts(array('post_status' =>'publish','post_type' => $postType,
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'term_id',
                        'terms' => $childTerm->term_id,
                    ),));
            ?>
            <div class="add-accordion">
                <h3><?php echo $childTerm->name ?></h3>
                <div class="add-accordion">
                    <?php foreach($posts as $post){ ?>
                        <h3><?php echo $post->post_title ?></h3>
                        <div class="">
                            <?php echo get_the_content($post->ID) ?>
                        </div>
                    <?php } ?>
                </div>
            </div>
            <?php
        }
    }
?>
</div>
<script>
    jQuery(function(){
        jQuery('.add-accordion').accordion({
            collapsible: true,
            heightStyle: "content",
            active: false,
            animate: 500,
            icons: false
        });
    });
</script>
<?php wp_enqueue_script('jquery-ui-accordion'); ?>
1
BlueSuiter

Ce code vous permet d'afficher une liste de toutes vos catégories, sous-catégories et publications dans chaque sous-catégorie. Assurez-vous de changer vos arguments avec votre propre taxonomie et votre type de message et lisez les commentaires dans le code pour plus de précisions:

function ow_categories_with_subcategories_and_posts( $taxonomy, $post_type ) {

    // Get the top categories that belong to the provided taxonomy (the ones without parent)
    $categories = get_terms( 
        array(
            'taxonomy'   => $taxonomy,
            'parent'     => 0, // <-- No Parent
            'orderby'    => 'term_id',
            'hide_empty' => true // <!-- change to false to also display empty ones
        )
    );
    ?>
    <div>
        <?php
        // Iterate through all categories to display each individual category
        foreach ( $categories as $category ) {

            $cat_name = $category->name;
            $cat_id   = $category->term_id;
            $cat_slug = $category->slug;

            // Display the name of each individual category
            echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug  . '</h3>'; 


            // Get all the subcategories that belong to the current category
            $subcategories = get_terms(
                array(
                    'taxonomy'   => $taxonomy,
                    'parent'     => $cat_id, // <-- The parent is the current category
                    'orderby'    => 'term_id',
                    'hide_empty' => true
                )
            );
            ?>
            <div>
                <?php
                // Iterate through all subcategories to display each individual subcategory
                foreach ( $subcategories as $subcategory ) {

                    $subcat_name = $subcategory->name;
                    $subcat_id   = $subcategory->term_id;
                    $subcat_slug = $subcategory->slug;

                    // Display the name of each individual subcategory with ID and Slug
                    echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';

                    // Get all posts that belong to this specific subcategory
                    $posts = new WP_Query(
                        array(
                            'post_type'      => $post_type,
                            'posts_per_page' => -1, // <-- Show all posts
                            'hide_empty'     => true,
                            'order'          => 'ASC',
                            'tax_query'      => array(
                                array(
                                    'taxonomy' => $taxonomy,
                                    'terms'    => $subcat_id,
                                    'field'    => 'id'
                                )
                            )
                        )
                    );

                    // If there are posts available within this subcategory
                    if ( $posts->have_posts() ):
                        ?>
                        <div>
                            <?php

                            // As long as there are posts to show
                            while ( $posts->have_posts() ): $posts->the_post();

                                //Show the title of each post with the Post ID
                                ?>
                                <p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
                                <?php

                            endwhile;
                            ?>
                        </div>
                        <?php
                    else:
                        echo 'No posts found';
                    endif;

                    wp_reset_query();
                }
                ?>
            </div>
            <?php
        }
        ?>
    </div>
    <?php
}
ow_categories_with_subcategories_and_posts( 'name_of_your_taxonomy', 'name_of_your_post_type' );
0
drjorgepolanco