• User Attivo

    Prova a scrivere questo codice, se non funziona prova a modificarlo. Non l'ho proprio testato e penso che sicuramente c'è qualche errore, in ogni caso penso tu debba fare una cosa del genere:

    <?php
    $sub_args = array(
    'child_of' => /* Qui va l'ID della categoria A */ ,);

    $subcategories = get_categories( $sub_args );

    for($i=0;isset($subcategories*);$i++) {
    $post_args = array(
    'posts_per_page' => 1,
    'category_name' => $subcategories*->name,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish', );

    $posts_array = get_posts( $post_args );

    foreach ($posts_array as $single_post) {
    setup_postdata($single_post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt();
    }
    }
    ?>


  • User Attivo

    Ovviamente dove sta scritto /* Qui va l'ID della categoria A / devi scrivere l'ID della categoria A e devi togliere / e */


  • User

    Grazie MarcoF., Gentilissimo

    Il codice più o meno funziona, ma mi fa vedere per 5 volte lo stesso articolo della categoria selezionata.

    Io sto provando a giocare con il codice, ma se trovi l'inghippo attendo

    :wink3: Grazie veramente molto


  • User Attivo

    Prova a riscrivere il foreach in questo modo:

    foreach ($posts_array as $single_post) {
    setup_postdata($single_post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt();
    wp_reset_postdata(); /* Aggiunta */
    }


  • User Attivo

    Ma ora ti fa vedere il primo articolo di una sola sottocategoria per 5 volte giusto?


  • User

    Si, anche con reset_postdata mi fa vedere il primo articolo di una sottocategoria per 5 volte.


  • User Attivo

    Ok allora ti dico che se tu scrivessi in questo modo, ti farebbe visualizzare i titoli giusti di ogni articolo:

    foreach ($posts_array as $single_post) {
    setup_postdata($single_post); ?>
    <h2><a href="<?php echo get_permalink($single_post->ID); ?>"><?php echo get_the_title($single_post->ID); ?></a></h2>
    <?php the_excerpt();
    wp_reset_postdata();
    }

    Il fatto è che non riesco a trovare un modo per recuperare il contenuto di un post tramite ID. Vedi se tu ci riesci.


  • User Attivo

    Anzi prova così:

    foreach ($posts_array as $single_post) {
    setup_postdata($single_post); ?>
    <h2><a href="<?php echo get_permalink($single_post->ID); ?>"><?php echo get_the_title($single_post->ID); ?></a></h2>
    <?php echo get_post($single_post->ID)->post_excerpt;
    wp_reset_postdata();
    }


  • User

    Ciao. Scusami per il ritardo ma per un paio di giorni non sono riuscito a lavorarci.

    Perfetto, funziona. Veramente molte grazie.

    Questo il nuovo codice, per le persone a cui potrebbe servire. Ciao

    <?php
    $sub_args = array(
    'child_of' => /* ID Categoria */ ,);

    $subcategories = get_categories( $sub_args );

    for($i=0;isset($subcategories*);$i++) {
    $post_args = array(
    'posts_per_page' => 1,
    'category_name' => $subcategories*->name,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish', );

    $posts_array = get_posts( $post_args ); foreach ($posts_array as $single_post) { setup_postdata($single_post); ?>
    <h2><a href="<?php echo get_permalink($single_post->ID); ?>"><?php echo get_the_title($single_post->ID); ?></a></h2>
    <?php the_excerpt(); wp_reset_postdata(); } }

    ?>


  • User Attivo

    Ottimo !!! 😉