Scorrere le tassonomie personalizzate e visualizzare i post

14 ago 2010, 17:37:31
Visualizzazioni: 63.1K
Voti: 8

Mentre sono riuscito a farlo funzionare per le normali categorie di WordPress, non sono riuscito a farlo funzionare per le tassonomie personalizzate.

Vorrei scorrere ogni tassonomia personalizzata (categorie nel mio caso) e produrre un numero di post per ciascuna.

Un esempio dell'output sarebbe:

Categoria 1

post dalla categoria uno 
post dalla categoria uno

leggi altro categoria uno


Categoria 2

post dalla categoria due
post dalla categoria due

leggi altro categoria due

Naturalmente si ripeterebbe attraverso qualsiasi tassonomia disponibile per il tipo di post personalizzato.

1
Commenti

Non capisco completamente la tua domanda. Quando chiedi "scorrere ogni tassonomia personalizzata" intendi "scorrere i termini di ogni tassonomia personalizzata?" Inoltre, quando chiedi di "produrre un numero di post", "produrre" significa inserire nuove istanze di un tipo di post nel database o generare HTML per il tipo di post?

MikeSchinkel MikeSchinkel
15 ago 2010 01:50:24
Tutte le risposte alla domanda 4
0
12

Ho pensato di fornire un'altra risposta poiché quella precedente è un po' approssimativa, inoltre ho aggiunto un ulteriore livello che recupera tutte le tassonomie per un post_type.

$post_type = 'post';

// Ottieni tutte le tassonomie per questo tipo di post
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

    // Ottieni ogni "categoria" (termine) in questa tassonomia per recuperare i relativi post
    $terms = get_terms( $taxonomy );

    foreach( $terms as $term ) : 

        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
            // Esegui qui il tuo loop di query generale  
        endwhile; endif;

    endforeach;

endforeach;

Sarebbe consigliabile aggiungere ogni post trovato a un array $post__not_in, in modo da poterlo passare alla WP_Query per evitare che vengano visualizzati post duplicati.

17 ago 2010 01:20:03
0

Stai cercando questo?

<?php query_posts(array('post_type' => 'nome del tipo di post', 'slug della tassonomia' => $term->slug ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Spiacenti, nessun post corrisponde ai tuoi criteri.'); ?></p>
<?php endif; ?>

Come creare un loop per Tassonomia Personalizzata

Spero ti sia utile

31 ago 2012 21:39:58
0

Copia e incolla questa funzione nel tuo file functions.php

if ( ! function_exists( 'display_all_products_from_all_categories' ) ) {

    function display_all_products_from_all_categories() {

        // Ottieni tutte le categorie per il Custom Post Type Product
        $args = array( 
            'post_type' => 'product', 
            'orderby' => 'id', 
            'order' => 'ASC' 
        );

        $categories = get_categories( $args );

        foreach ($categories as $category) {
            ?>
            <div class="<?php echo $category->slug; ?>">
                <!-- Ottieni il titolo della categoria -->
                <h3 class="title"><?php echo $category->name; ?></h3>

                <!-- Ottieni la descrizione della categoria -->
                <div class="description">
                    <p><?php echo category_description( get_category_by_slug($category->slug)->term_id ); ?></p>
                </div>

                <ul class="mhc-product-grid">

                    <?php
                        // Ottieni tutti i prodotti di ogni categoria specifica
                        $product_args = array(
                            'post_type'     => 'product',
                            'orderby'      => 'id',
                            'order'         => 'ASC',
                            'post_status'   => 'publish',
                            'category_name' => $category->slug //passando lo slug della categoria corrente
                        );

                        $product_list = new WP_Query ( $product_args );

                    ?>

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

                        <li class="product <?php the_field( 'product_flavor' ); ?>">
                            <a href="<?php the_permalink(); ?>" class="product-link">

                                <!-- se il post ha un'immagine, mostrala -->
                                <?php if( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail( 'full', array( 'class' => 'img', 'alt' => get_the_title() ) ); ?>
                                <?php endif; ?>

                                <!-- campi personalizzati: product_flavor, product_description ... -->
                                <h3 class="title <?php the_field( 'product_flavor' ); ?>"><?php the_title(); ?></h3>
                                <p class="description"><?php the_field( 'product_description' ); ?></p>
                            </a>
                        </li>

                    <?php endwhile; wp_reset_query(); ?>
                </ul>

            </div>
            <?php
        }
    }
}

Poi chiamala da qualsiasi punto del tuo template con:

display_all_products_from_all_categories();

3 ago 2016 21:28:13
0
-1

Per favore controlla questo esempio; crea un loop personalizzato per la tua tassonomia. Puoi anche utilizzarlo in un ciclo foreach per usare tutte le categorie. Oppure devi creare una query SQL personalizzata.

<?php
$taxonomies = get_the_term_list($post->ID, 'YOUR_TAXONOMIE', '', '', '');
$taxonomies = explode('>', $taxonomies);
$taxonomies = $taxonomies[1];
$myq = new WP_Query('your_taxonomie = '.$taxonomies); 
if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); // the loop ?>

            <?php the_title();?>
            <?php the_content();?>

<?php endwhile; else:?>

<?php endif;?>
15 ago 2010 00:28:28