Come ottenere l'elenco dei post delle categorie figlie in un singolo template?

15 mag 2011, 06:02:05
Visualizzazioni: 13.8K
Voti: 0

Ecco quello che voglio ottenere

La mia struttura di categorie nell'amministrazione di WP

Cat Parent
  -Cat Child 1
  -Cat Child 2
  -Cat Child 3
  -Cat Child 1

Questo è quello che voglio ottenere in un singolo template:

Deve mostrare l'elenco dei post nelle categorie figlie di Cat Parent

Cat Child 1
 -Post1 Child 1
 -Post2 Child 1
 -Post3 Child 1
 -E così via... .

Cat Child 2
 -Post1 Child 2
 -Post2 Child 2
 -Post3 Child 2
 -E così via... .

Cat Child 3
 -Post1 Child 3
 -Post2 Child 3
 -Post3 Child 3
 -E così via... .

Qualcuno l'ha già fatto prima o ha un'idea su come codificarlo?

Grazie

1
Commenti

Forse non ho capito bene la tua domanda. Vuoi elencare i post dalle categorie figlie quando accedi all'elenco della categoria genitore? È questo che stai chiedendo? Se sì, questo è esattamente ciò che accade già in WordPress. O forse vuoi che i post siano elencati in ordine di categorie figlie?

Hameedullah Khan Hameedullah Khan
15 mag 2011 12:34:04
Tutte le risposte alla domanda 3
3

ottieni le categorie figlie utilizzando get_categories(); poi scorrile con un ciclo foreach, usando WP_Query():

<?php  $cats = get_categories('child_of='.get_query_var('cat')); 

    foreach ($cats as $cat) :

    $args = array(
    'posts_per_page' => 3, // numero massimo di articoli per categoria
    'category__in' => array($cat->term_id)
    );
    $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) : 
        echo '<h3>'.$cat->name.'</h3>';

        while ($my_query->have_posts()) : $my_query->the_post(); ?>     
        <?php /* output generale del loop; ad esempio: */ ?>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  

        <?php endwhile; ?>

        <?php else : 
        echo 'Nessun articolo per '.$cat->name;                
        endif; 

    endforeach; ?>
15 mag 2011 10:55:23
Commenti

L'uso di wp_reset_query non è corretto, viene utilizzato per reimpostare la query del loop principale, ma qui hai creato una seconda query e chiamare wp_reset_query non avrà alcun effetto sulla tua $my_query.

Hameedullah Khan Hameedullah Khan
15 mag 2011 12:35:02

@Hameedullah Khan grazie per il commento; l'ho rimosso dal codice sopra. Se necessario, bisognerebbe aggiungere del codice per preservare la query originale al codice sopra.

Michael Michael
15 mag 2011 14:05:38

Fantastico!! funziona davvero bene!! mi hai aiutato moltissimo Grazie mille Michael!!

idontknowhow idontknowhow
16 mag 2011 17:11:42
1
-2

Ciclo tassonomia genitore-figlio

$taxonomy = 'product_cat';
$category = get_terms($taxonomy);

echo '<ul>';

foreach( $category as $cat ){
    $link = get_term_link( $cat->slug, $taxonomy );

    if(empty($cat->parent)){
        echo '<li><a href="' . $link . '">' . $cat->name . '</a>' . '<strong>' . $cat->count . '</strong></li>';

    }
    $loop = 0;

    foreach( $category as $par ){
        $link = get_term_link( $par->slug, $taxonomy );
        if($cat->term_id == $par->parent ){

        if($loop == 0){ echo '<ul>'; }
        echo '<li><a href="' . $link . '">' . $par->name . '</a>' . $par->count . '</li>';

        $loop++;
        }
    }
    if($loop > 0){ echo '</ul>'; }

}

echo '</ul>';
28 mag 2016 08:28:43
Commenti

Spiega il codice o fornisci alcuni suggerimenti, semplicemente pubblicare il codice non è sufficiente.

bravokeyl bravokeyl
28 mag 2016 08:49:43
1
-3
<ul class="catTags">
<?php
        $args = array(
        'show_option_all'    => '',
        'orderby'            => 'count',
        'order'              => 'DESC',
        'style'              => 'list',
        'show_count'         => 0,
        'hide_empty'         => 1,
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'feed'               => '',
        'feed_type'          => '',
        'feed_image'         => '',
        'exclude'            => 1,
        'exclude_tree'       => '',
        'include'            => '',
        'hierarchical'       => 1,
        'title_li'           => __( '' ),
        'show_option_none'   => __('Nessuna categoria'),
        'number'             => null,
        'echo'               => 1,
        'depth'              => 2,
        'current_category'   => 0,
        'pad_counts'         => 0,
        'taxonomy'           => 'category',
        'walker'             => null    
        ); 

        wp_list_categories( $args );
    ?>
    </ul>   
    </div>

    <?php get_sidebar(); ?>

E il CSS :

section .primary ul.catTags {
 padding:0px;
}

section .primary ul.catTags li {
 float: left;
 list-style-type: none;
 margin: 0px 20px 10px 20px;

}

section .primary ul.catTags li a,section .primary ul.catTags li a:visited {
 font-size:18px;
 text-decoration:underline;
}
section .primary ul.catTags li a:hover {
 text-decoration:none;
}
section .primary ul.catTags li ul.children {
 max-width:200px;
}
section .primary ul.catTags li ul.children li {
 margin: 0px 5px 3px 5px;
 display: inline;
}
section .primary ul.catTags li ul.children li a, section .primary ul.catTags li ul.children li a:visited {
 font-size:14px;
 text-decoration:none;
}
section .primary ul.catTags li ul.children li a:hover {
 text-decoration:underline;
}
30 mag 2013 10:42:15
Commenti

Per favore aggiungi una spiegazione alla tua risposta: perché potrebbe risolvere il problema?

fuxia fuxia
30 mag 2013 10:53:16