Supporto » Funzionalità = Plugin » Conteggio caratteri estratto articoli

  • Risolto zippo978

    (@zippo978)


    Buon giorno a tutti,

    premesso che non so molto di PHP vi espongo il mio problema:

    sto finendo di modificare un blog in WordPress per mia moglie, l’ ultima cosa che mi manca di fare è creare un estratto degli articoli con lo stesso numero di caratteri, non di parole perchè l’ anteprima non viene precisa, quindi pensavo al conteggio delle lettere cosi da permettere una visualizzazione omogenea (tutti gli estratti delle stesse dimensioni), ho provato mille funzioni da mettere nel file function.php ma tutte quelle che ho provato, anche quelle che dicevano di contare i caratteri contano le parole, al momento uso questa funzione:

    function wpse_allowedtags() {
        // Add custom tags to this string
            return '<script>,<style>,<br>,<pre>,<em>,<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 = 15;
                    $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) { 
    
                        if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) { 
                        // Limit reached, continue until , ; ? . or ! occur at the end
                            $excerptOutput .= trim($token);
                            break;
                        }
    
                        // Add words to complete sentence
                        $count++;
    
                        // Append what's left of the token
                        $excerptOutput .= $token;
                    }
    
                 $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'); 
    

    che oltre a contare le parole mantiene la formattazione e mantiene parole intere.

    C’è una possibilità di fare quello che cerco mantenendo la funzione? O comunque una funzione che fa tutto quello che cerco ?

    Grazie a tutti in anticipo

    P.s. Dimenticavo Uso WordPress 4.6, il tema modificato è twenty sixteen, non vorrei usare Plugin.

    il sito dove faccio tutte le modifiche è http://etherealprove.altervista.org/

Stai visualizzando 3 risposte - dal 16 al 18 (di 18 totali)
  • Per fare questa richiesta devi aprire un nuovo thread. Questo è da considerarsi chiuso. Comunque secondo il mio modesto parere quello che chiedi non è banale.
    Giancarlo

    Salve @zippo978,

    puoi farlo anche con del CSS, tieni presente che non è garantito funzioni in modo uguale su tutti i browser. Prova.

    Ad esempio con questo codice tutti i paragrafi figli del elemento con classe “.testo-limitato” sono limitati a 2 righe o massimo 32px.

    .testo-limitato p {
       overflow: hidden;
       text-overflow: ellipsis;
       display: -webkit-box;
       line-height: 16px;
       max-height: 32px;
       -webkit-line-clamp: 2;
       -webkit-box-orient: vertical;
    }

    https://jsfiddle.net/agm65/nrkgd6jr/

    Saluti,
    Diego

    Chi ha creato la discussione zippo978

    (@zippo978)

    Ragazzi scusate se rispondo solo ora ma mi è mancato il tempo.

    @gmosso

    Grazie di tutto, aprirò una nuova discussione e vediamo se quello che cerco è fattibile 😀

    @agm65

    Al CSS ci avevo pensato ma non è supportato da tutti i browser e le soluzioni per renderlo compatibile non hanno la precisione che cerco ma grazie comunque.

    Ciao

Stai visualizzando 3 risposte - dal 16 al 18 (di 18 totali)
  • Il topic ‘Conteggio caratteri estratto articoli’ è chiuso a nuove risposte.