WordPress: mostrare un estratto delle pagine figlie

WordPress: mostrare un estratto delle pagine figlie

Le pagine di WordPress vengono ordinate secondo una gerarchia. Qualora le pagine che discendono dalla pagina genitrice non fossero raggiungibili dall'utente mediante i menu di navigazione, possiamo mostrarne un estratto utilizzando uno shortcode. Vediamo come fare.

Aggiungete il seguente codice al file functions.php:


function subpage_excerpt() {

	global $post;

	
	$args = array(
		'post_parent' => $post->ID,
		'post_type' => 'page'
	);
	
	$subpages = new WP_Query($args);

	
	if ($subpages->have_posts()) :
		$output = '<ul>';
		while ($subpages->have_posts()) : $subpages->the_post();
			$output .= '<li><strong><a href="'.get_permalink().'">'.get_the_title().'</a></strong>
						<p>'.get_the_excerpt().'</p></li>';
		endwhile;
		$output .= '</ul>';
	else :
		$output = '<p>Nessuna pagina trovata.</p>';
	endif;

	
	wp_reset_postdata();

	return $output;
}

add_shortcode('subpage-excerpt', 'subpage_excerpt');

Quindi inserite lo shortcode creato nella pagina da voi scelta:

[subpage-excerpt]
Torna su