Utiliser wp_trim_excerpt pour extraire the_excerpt () de la boucle
-
-
Vouspouvezessayer d'appeler lesfiltres d'extraits ... `$myvar=apply_filters ('the_excerpt',$myvar);`You could try calling the excerpt filters... `$myvar = apply_filters( 'the_excerpt', $myvar );`
- 0
- 2011-01-15
- t31os
-
7 réponses
- votes
-
- 2012-03-22
Depuis WP 3.3.0,
wp_trim_words()
est utile si vous êtesen mesure d'obtenir le contenupour lequel vous souhaitezgénérer unextrait.J'espère que cela sera utile à quelqu'unet que cela vous évitera de créer votreproprefonction de comptage demots.Since WP 3.3.0,
wp_trim_words()
is helpful if you're able to get the content that you want to generate an excerpt for. Hope that's helpful to someone and it saves creating your own word counting function. -
- 2011-01-14
wp_trim_excerpt()
a unemécanique unpeu curieuse - si quelque chose luiestpassé,ilne fait rien.Voici la logique debase derrière cela:
-
get_the_excerpt()
recherche unextraitmanuel; -
wp_trim_excerpt()
intervient s'iln'y apas d'extraitmanuelet en crée un àpartir du contenu ou duteaser.
Les deux sont étroitement liés aux variablesglobaleset donc Loop.
En dehors de laboucle,vousferiezmieux de retirer le code de
wp_trim_excerpt()
et d'écrire votreproprefonction de découpage.wp_trim_excerpt()
has a little curious mechanics - if anything is passed to it then it does nothing.Here is basic logic behind it:
get_the_excerpt()
checks for manual excerpt;wp_trim_excerpt()
chimes in if there is no manual excerpt and makes one from content or teaser.
Both are tightly tied to global variables and so Loop.
Outside the Loop you are better of taking code out of
wp_trim_excerpt()
and writing your own trim function. -
- 2011-01-21
Mise àjour:
Voici un dérivé de wp_trim_excerpt () quej'ai utilisé.Marcheparfaitement.Dérivé de la version 3.0.4 de Wordpress
function my_excerpt($text, $excerpt) { if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
Update:
Here is a derivative of wp_trim_excerpt() which I used. Works perfectly. Derived from Wordpress version 3.0.4
function my_excerpt($text, $excerpt) { if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
Vousn'êtespas obligé depublier unenouvelle réponse,vouspouveztoujoursmodifier l'anciennepourinclure denouvellesinformations.Vouspouvez,parexemple,copier le lien vers le code WP de votrepremière réponse dans celle-ci,puis supprimer votrepremière réponse.You don't have to post a new answer, you can always edit your old one to include new information. You could, for example, copy the link to the WP code from your first answer into this one and then delete your first answer.
- 0
- 2011-01-25
- Jan Fabry
-
Pour les copieurs/pasteurs: ajoutez $ raw_excerpt=$text;For copy/pasters out there: add $raw_excerpt = $text;
- 0
- 2015-04-11
- Svetoslav Marinov
-
- 2011-08-26
Voicimonpoint de vue sur un "trim_excerpt" quiprend l'objet depublication ou un ID depublication commeparamètre.
Évidemmentbasé sur ce quiest au cœur. Jene saispaspourquoi cela (etget_the_author ())n'ontpas d'équivalents sansboucle.
/** * Generates an excerpt from the content, if needed. * * @param int|object $post_or_id can be the post ID, or the actual $post object itself * @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it * @return string the snipped excerpt or the manual excerpt if it exists */ function zg_trim_excerpt($post_or_id, $excerpt_more = ' [...]') { if ( is_object( $post_or_id ) ) $postObj = $post_or_id; else $postObj = get_post($post_or_id); $raw_excerpt = $text = $postObj->post_excerpt; if ( '' == $text ) { $text = $postObj->post_content; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); // don't automatically assume we will be using the global "read more" link provided by the theme // $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
Here's my take on a "trim_excerpt" that takes the post object or a post ID as a parameter.
Obviously based on what's in core. Don't know why this (and get_the_author()) don't have non-loop equivalents.
/** * Generates an excerpt from the content, if needed. * * @param int|object $post_or_id can be the post ID, or the actual $post object itself * @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it * @return string the snipped excerpt or the manual excerpt if it exists */ function zg_trim_excerpt($post_or_id, $excerpt_more = ' [...]') { if ( is_object( $post_or_id ) ) $postObj = $post_or_id; else $postObj = get_post($post_or_id); $raw_excerpt = $text = $postObj->post_excerpt; if ( '' == $text ) { $text = $postObj->post_content; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); // don't automatically assume we will be using the global "read more" link provided by the theme // $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
- 2011-01-21
+1 à Rast. Ilesttrès étrange qu'iln'y aitpas de chosetelle queget_the_excerpt ($post-> ID),alors que cela devrait être assez évident. Quoi qu'ilen soit,voici wp_trim_excerpt () dans wordpress version 3.0.4:
http://core.trac. wordpress.org/browser/tags/3.0.4/wp-includes/formatting.php
function wp_trim_excerpt($text) { 1824 $raw_excerpt = $text; 1825 if ( '' == $text ) { 1826 $text = get_the_content(''); 1827 1828 $text = strip_shortcodes( $text ); 1829 1830 $text = apply_filters('the_content', $text); 1831 $text = str_replace(']]>', ']]>', $text); 1832 $text = strip_tags($text); 1833 $excerpt_length = apply_filters('excerpt_length', 55); 1834 $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); 1835 $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 1836 if ( count($words) > $excerpt_length ) { 1837 array_pop($words); 1838 $text = implode(' ', $words); 1839 $text = $text . $excerpt_more; 1840 } else { 1841 $text = implode(' ', $words); 1842 } 1843 } 1844 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 1845 }
Vouspouvez voir à la ligne 1826 qu'ilest lié à la variableglobale $post viaget_the_contents. Et oui,je n'ai aucuneidée de ce qu'ilspensaient. Mais àpartir de là,remplacez leget_the_contentpar $text dans votrepropremy_excerpt,et il devrait se comporter de lamêmemanière.
+1 to Rast. It is very weird that there is no such thing as get_the_excerpt($post->ID), when it should be quite obvious that it should. Anyway, here is wp_trim_excerpt() in wordpress version 3.0.4:
http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/formatting.php
function wp_trim_excerpt($text) { 1824 $raw_excerpt = $text; 1825 if ( '' == $text ) { 1826 $text = get_the_content(''); 1827 1828 $text = strip_shortcodes( $text ); 1829 1830 $text = apply_filters('the_content', $text); 1831 $text = str_replace(']]>', ']]>', $text); 1832 $text = strip_tags($text); 1833 $excerpt_length = apply_filters('excerpt_length', 55); 1834 $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); 1835 $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 1836 if ( count($words) > $excerpt_length ) { 1837 array_pop($words); 1838 $text = implode(' ', $words); 1839 $text = $text . $excerpt_more; 1840 } else { 1841 $text = implode(' ', $words); 1842 } 1843 } 1844 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 1845 }
You can see on line 1826 that it is linked to the $post global variable via get_the_contents. And yes, I have no idea what were they thinking. But from here, replace the get_the_content with $text in your own my_excerpt, and it should behave in a similar fashion.
-
- 2011-04-17
Lafonctionget_the_content () renverrait le contenu complet si $more!=0. Vous devez définir la variableglobale $more sur 0pour vous assurer que lafonctionget_the_content () renvoie unextrait.
Fonction wp_trim_excerpt ()modifiée:
function wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { global $more; $tmp = $more; $more = 0; $text = get_the_content(''); $more = $tmp; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
The get_the_content() function would return full content if $more != 0. You have to set global variable $more to 0 to make sure get_the_content() function return excerpt.
Modified wp_trim_excerpt() function:
function wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { global $more; $tmp = $more; $more = 0; $text = get_the_content(''); $more = $tmp; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
- 2016-09-15
En utilisant les réponses des autres ci-dessus,voici une réponseplus simple qui semblebien fonctionner:
global $post; $excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post->ID)); if ( $excerpt == '' ) { $excerpt = wp_trim_words( $post->post_content, 55 ); }
Je l'utilise dans lesbalises
<meta>
d'unefonctionpour définir des descriptions OpenGraph.Alorsj'ajoute simplement:<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />
Using others' answers above, here's a simpler answer that seems to work well:
global $post; $excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post->ID)); if ( $excerpt == '' ) { $excerpt = wp_trim_words( $post->post_content, 55 ); }
I'm using it in the
<meta>
tags in a function to define OpenGraph descriptions. So then I just add:<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />
-
Qu'enest-il du contenu HTML?Comment celatraitera-t-il lestags?L'extrait supprime également lesbalises htmlet les codes courts.et si lespremiersmots de l'extrait contenaient uneimage?Cela casseraprobablement votremiseen page.What about HTML content? How will this deal with tags? the excerpt also strips html tags and shortcodes. what if the first words of the excerpt contain an image? That will probably break your layout.
- 0
- 2019-12-04
- brett
Je suisen train de créer unthème qui affichera desextraits sur lapage d'accueilpourpotentiellement des dizaines d'articles. Jen'aipas d'extraitsmanuels surtousmesmessages,donc
$post->post_excerpt
est videpour denombreuxmessages. Dans le cas oùiln'y auraitpas d'extraitmanuel,je voudrais utiliser lafonctionintégréeget_the_excerpt (),maisellen'estpas disponibleen dehors de laboucle.En suivant lafonction,il semble qu'elle utilise wp_trim_excerpt de wp-includes/formatting.phppour créer desextraits à la volée. Je l'appelle dansmon code comme
wp_trim_excerpt( $item->post_content )
,maisil renvoie simplement le contenu complet. Est-ce queje fais quelque chose demal?Je sais queje peux créermaproprefonctionpour créer unextrait,maisj'aime utiliser lesfonctionsintégrées lorsque celaestpossible,en gardantmon code compatible avec d'autresplugins/filtrespotentiels.
http://adambrown.info/p/wp_hooks/hook/wp_trim_excerpt? version=3.0 & amp;file=wp-includes/formatting.php