Estratto articoli conteggio righe
-
Salve a tutti,
quello che mi piacerebbe fare è avere gli estratti degli articoli aventi tutti lo stesso numero di righe per avere una Home, dato che si sviluppa su due colonne, piu ordinata e precisa e non uno piu alto ed uno piu basso.
Il codice che utilizzo per l’ excerpt è
function wpse_allowedtags() { // Add custom tags to this string return '<script>,<style>,<br>,<em>,<pre>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>'; } if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) : function wpse_custom_wp_trim_excerpt($wpse_excerpt) { global $post; $raw_excerpt = $wpse_excerpt; if ( '' == $wpse_excerpt ) { $wpse_excerpt = get_the_content(''); $wpse_excerpt = strip_shortcodes( $wpse_excerpt ); $wpse_excerpt = apply_filters('the_content', $wpse_excerpt); $wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt); $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */ //Set the excerpt word count and only break after sentence is complete. $excerpt_word_count = 300; $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); $tokens = array(); $excerptOutput = ''; $count = 0; // Divide the string into tokens; HTML tags, or words, followed by any whitespace preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens); foreach ($tokens[0] as $token) { $excerptOutput .= $token; $excerptOutputWithoutTags = strip_tags(trim($excerptOutput)); if (strlen($excerptOutputWithoutTags) >= $excerpt_word_count){ break; } } $wpse_excerpt = trim(force_balance_tags($excerptOutput)); $excerpt_end = '... <br/><br/> <a style="font-family: Merriweather, Georgia, serif; font-size: 0.75rem; text-transform: uppercase;" href="'. esc_url( get_permalink() ) . '">' . '' . sprintf(__( 'Read more: %s ', 'wpse' ), get_the_title()) . '</a>'; $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); $pos = strrpos($wpse_excerpt, '</'); if ($pos !== false) // Inside last HTML tag $wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */ else // After the content $wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */ return $wpse_excerpt; } return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt); } endif; remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
” Grazie a @gmosso per avermi aiutato nelle modifiche ”
Questo codice stampa un tot di caratteri dell’ estratto degli articoli e ne mantiene la formattazione, il problema è che anche con lo stesso numero di caratteri gli estratti non sono della stessa altezza a causa degli accapo negli articoli stessi, ora mi chiedevo è possibile limitare l’ estratto ad un certo numero massimo di righe ? Tipo 300 caratteri ma per un massimo di che ne so… 9/10 righe ?
Grazie a tutti in anticipo.
P.s. Sono aperto a qualsiasi idea che mi aiuti nell’ intento 🙂
- Il topic ‘Estratto articoli conteggio righe’ è chiuso a nuove risposte.