Vuoi mostrare il conteggio totale delle parole pubblicate in fondo al widget “Right Now” nella bacheca di WordPress? Sebbene esista probabilmente un plugin per questo, abbiamo creato un rapido snippet di codice che puoi utilizzare per visualizzare il conteggio delle parole dei post in WordPress.
Istruzioni:
Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema o in un plugin specifico per il sito:
function post_word_count() {
$count = 0;
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array( 'post', 'page' )
));
foreach( $posts as $post ) {
$count += str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
}
$num = number_format_i18n( $count );
// This block will add your word count to the stats portion of the Right Now box
$text = _n( 'Word', 'Words', $num );
echo "<tr><td class='first b'>{$num}</td><td class='t'>{$text}</td></tr>";
// This line will add your word count to the bottom of the Right Now box.
echo '<p>This blog contains a total of <strong>' . $num . '</strong> published words!</p>';
}
// add to Content Stats table
add_action( 'right_now_content_table_end', 'post_word_count');
// add to bottom of Activity Box
add_action('activity_box_end', 'post_word_count');
Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come aggiungere correttamente snippet di codice in WordPress, in modo da non compromettere accidentalmente il tuo sito.
Se ti è piaciuto questo snippet di codice, prendi in considerazione la lettura dei nostri altri articoli sul sito come: I 7 migliori plugin per sondaggi di WordPress per aumentare l'interazione sul sito e I 12 migliori plugin per podcast di WordPress.
È possibile aggiungerlo al blog effettivo con un tag template?
Ciao Justin, sì, puoi farlo, dovresti essere in grado di aggiungerlo al tuo file template e questo verrà visualizzato. Tieni presente che questo script aggiunge HTML che vorrai modificare affinché funzioni bene nel tuo tema.
[...] Questo post è stato menzionato su Twitter da Michael Davis, WPSNIPP. WPSNIPP ha detto: #wordpress Conteggio parole post http://bit.ly/gULp1N #blog per favore RT :)