Ottenere il primo paragrafo di ogni post
Ho del codice che utilizza preg_match per estrarre il primo paragrafo e poi lo visualizza nel loop:
function first_paragraph() {
global $post, $posts;
$first_para = '';
ob_start();
ob_end_clean();
$output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post->post_content, $matches);
$first_para = $matches [1] [0];
echo $first_para;
}
Tuttavia, c'è un piccolo problema con questo codice. Funziona solo quando avvolgo manualmente il testo nei tag <p> </p>
nell'editor, e non altrimenti. Le espressioni regolari non sono certo il mio forte, quindi qualsiasi aiuto o maggiore comprensione sarebbe fantastico.
E questo funziona, includendo anche immagini/iframe nei tag p:
function first_paragraph() {
global $post, $posts;
$first_para = '';
ob_start();
ob_end_clean();
$post_content = $post->post_content;
$post_content = apply_filters('the_content', $post_content);
$output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post_content, $matches);
$first_para = $matches [1] [0];
echo $first_para;
}

Puoi utilizzare questa funzione:
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';
}
e poi richiamarla nel tuo loop con:
<?php echo get_first_paragraph(); ?>
La parte magica che stai cercando è wpautop, una funzione di WordPress, che convertirà i doppi a-capo nel testo in paragrafi corretti.
Con wpautop attivo, puoi quindi usare la funzione PHP substr per ottenere il primo paragrafo partendo dal primo carattere fino a raggiungere la chiusura del primo paragrafo, e poi aggiungere 4 caratteri così che il tag di chiusura non venga rimosso.
Per espandere ulteriormente, se vuoi ottenere tutto tranne il primo paragrafo puoi usare questa funzione complementare che inizierà dalla fine del primo tag di chiusura del paragrafo e prenderà tutto ciò che viene dopo:
function get_the_post(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, (strpos( $str, '</p>')));
return $str;
}
e richiamarla nel loop con:
<?php echo get_the_post(); ?>

Prova questo:
function first_paragraph() {
global $post, $posts;
$post_content = $post->post_content;
$post_content = apply_filters('the_content', $post_content);
$post_content = str_replace('</p>', '', $post_content);
$paras = explode('<p>', $post_content);
array_shift($paras);
return $paras[0];
}

No! Non produce nulla, anche quando aggiungo manualmente <p> nell'editor. Ha qualcosa a che fare con il fatto che post_content non formatta come the_content()?

WordPress aggiungerà automaticamente i tag <p>
al tuo contenuto, questo è ciò che fa apply_filters('the_content');
insieme a wpautop
, strano che non funzioni dato che l'ho preso da un sito su cui ho lavorato di recente ma prendeva i primi due paragrafi così potevo metterli in 2 colonne.. Non sono sicuro a questo punto - devi fornirmi più codice (riguardo al loop ecc. dove vuoi mostrare il contenuto).
