Query wp per ottenere le pagine figlie della pagina corrente

31 lug 2012, 12:14:19
Visualizzazioni: 165K
Voti: 55

Qualcuno può aiutarmi con la wp_query?

Sto creando un file template/loop per creare una pagina archivio delle pagine figlie della pagina corrente.

Questa query deve essere automatica poiché la sto utilizzando in diverse pagine.

Questa è la mia query qui sotto, ma restituisce solo i miei post invece delle pagine figlie.

<?php

$parent = new WP_Query(array(

    'post_parent'       => $post->ID,                               
    'order'             => 'ASC',
    'orderby'           => 'menu_order',
    'posts_per_page'    => -1,
    'post_type'         => 'page' // Aggiunto per ottenere solo le pagine

));

if ($parent->have_posts()) : ?>

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

        <div id="parent-<?php the_ID(); ?>" class="parent-page">                                

            <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

            <p><?php the_advanced_excerpt(); ?></p>

        </div>  

    <?php endwhile; ?>

<?php unset($parent); endif; wp_reset_postdata(); ?>

Grazie in anticipo per qualsiasi aiuto.

Josh

1
Commenti

Prova questa soluzione

ottenere i figli di un post - http://wordpress.stackexchange.com/a/123143/42702

T.Todua T.Todua
13 nov 2013 12:36:39
Tutte le risposte alla domanda 4
2
101

Devi cambiare child_of in post_parent e inoltre aggiungere post_type => 'page':

WordPress codex Wp_query Parametri Post & Pagina

<?php

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

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

        <div id="parent-<?php the_ID(); ?>" class="parent-page">

            <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

            <p><?php the_advanced_excerpt(); ?></p>

        </div>

    <?php endwhile; ?>

<?php endif; wp_reset_postdata(); ?>
31 lug 2012 12:23:25
Commenti

Grazie amico, ho provato post_parent originale ma è 'post_type' => 'page' la chiave - le query di WordPress usano come default i post allora? Accetterò la risposta quando me lo permetterà.

Joshc Joshc
31 lug 2012 12:27:44

Sì, 'post_type' => 'post' è il valore predefinito.

mrwweb mrwweb
26 mar 2019 17:01:21
1

Sono consapevole che questa sia una domanda molto vecchia, ma dato che ci sono capitato, potrebbe succedere anche ad altri.

WordPress offre una soluzione molto semplice per elencare le pagine, dove è possibile aggiungere anche alcuni argomenti.

Questo è tutto ciò che ti servirà per visualizzare le pagine figlie:

wp_list_pages(array(
  'child_of' => $post->ID,
  'title_li' => ''
))

Consulta la pagina di riferimento per wp_list_pages per tutte le opzioni che puoi applicare.

26 feb 2020 12:40:12
Commenti

Questo restituirà una stringa HTML anziché un elenco di oggetti post, quindi probabilmente non è ciò che l'OP desidera.

Alexander Holsgrove Alexander Holsgrove
27 lug 2020 14:43:44
0

Per riscrivere questo codice come una funzione in functions.php, è necessario aggiungere global $post;

function page_summary() {
    global $post;
    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => $post->ID,
        'order'          => 'ASC',
        'orderby'        => 'menu_order'
    );


    $parent = new WP_Query( $args );

    if ( $parent->have_posts() ) : 

        while ( $parent->have_posts() ) : $parent->the_post(); ?>
        <div id="parent-<?php the_ID(); ?>" class="parent-page">

        <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
        </div>
    <?php
         endwhile; 

    endif; 
    wp_reset_postdata(); 
}
5 feb 2020 21:44:08
0

Sebbene l'OP abbia chiesto specificamente per una wp_query, funziona anche questo:

get_pages('child_of='.$post->ID.'&sort_column=menu_order');

22 ott 2020 23:44:39