WordPress: la funzione get_post_format()

WordPress: la funzione get_post_format()

I formati dei post sono stati introdotti in WordPress 3.0 per rispondere alle esigenze degli autori che chiedevano maggiore flessibilità nel modo con cui i post vengono organizzati e visualizzati. Una funzione fondamentale per gestire i formati dei post è get_post_format(). Vediamola in dettaglio.

Uso di base


<?php $format = get_post_format( $post_id ); ?>

Parametri e valori

  • $post_id: l'ID del post selezionato (se manca viene usato il post corrente del Loop)
  • $format: restituisce uno dei seguenti valori:
    • aside
    • chat
    • gallery
    • link
    • image
    • quote
    • status
    • video
    • audio

Esempio

Un esempio pratico è la selezione di un template diverso per i post a seconda del formato usato:


<?php
/* Dovete usare una struttura come format-link.php, format-aside.php ecc. (format deve essere presente).
 * Da usare nel Loop.
 */

$format = get_post_format();
get_template_part( 'format', $format );
?>
Torna su