Comment récupérer du texte uniquement à partir de wp_content () et non de wp_excerpt ()?
5 réponses
- votes
-
- 2015-09-30
Ouencoreplus simple:
echo wp_strip_all_tags( get_the_content() );
En utilisant:
-
get_the_content()
Récupérez le contenu dumessage.(Doit être utilisé dans uneboucle)
Une différenceimportantepar rapport à
the_content()
est queget_the_content()
ne transmetpas le contenu via le 'the_content
'.Cela signifie queget_the_content()
n'intègrerapas automatiquement les vidéos oune développerapas les codes courts,entre autres. -
wp_strip_all_tags()
Supprimez correctementtoutes lesbalises HTML,y compris le scriptet le style.
Or even simpler:
echo wp_strip_all_tags( get_the_content() );
By using:
get_the_content()
Retrieve the post content. (Must be used in a Loop)
An important difference from
the_content()
is thatget_the_content()
does not pass the content through the 'the_content
'. This means thatget_the_content()
will not auto-embed videos or expand shortcodes, among other things.wp_strip_all_tags()
Properly strip all HTML tags including script and style.
-
Cela devrait être approuvé comme réponse.This should be approved as the answer.
- 0
- 2018-04-08
- AlbertSamuel
-
Celane couvrepasnonplus les codes courts,enveloppez-le dans `` strip_shortcodes () `` ``It's also does not cover shortcodes, wrap it into ```strip_shortcodes()```
- 0
- 2020-05-25
- Ostap B.
-
- 2013-02-22
Iln'y apas defonction WordPressnativepour récupérer uniquement dutexte,mais vouspouvez utiliser desfiltres WordPresset du code regexpour cibler ceproblème spécifique.
Pour obtenir dutextenonformaté,utilisez la fonctionget_the_content () . Pour appliquertous lesfiltres,utilisez-le de cettefaçon (voir codex: http://codex.wordpress.org/Function_Reference/the_content # Alternative_Usage ):
$content = get_the_content(); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Avant d'appliquer desfiltres,il y a unespacepour vosmodificationspersonnalisées,parexemple supprimer desimages. Parici:
$content = get_the_content(); $content = preg_replace('/(<)([img])(\w+)([^>]*>)/', "", $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Source du codepreg_replace: http://www.phpzag.com/php-remove-image-tags-from-a-html-string-with-preg_replace/
Vous devrezpeut-être supprimer également les codes courts,le cas échéant. Celapeut également êtrefait viapreg_replaceet jeparie que vousen trouverez sur Google.
There's not a native WordPress function to retrieve text only, but you can use WordPress filters and regex code to target this specific problem.
To get unformated text, use get_the_content() function. To apply all filters, use it this way( see codex: http://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage ):
$content = get_the_content(); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Before applying filters, there is a space for you custom modifications, e.g. removing images. This way:
$content = get_the_content(); $content = preg_replace('/(<)([img])(\w+)([^>]*>)/', "", $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Source of preg_replace code: http://www.phpzag.com/php-remove-image-tags-from-a-html-string-with-preg_replace/
You may need to remove also shortcodes, if any are used. This can be done also via preg_replace and I bet you'll find some on the google.
-
Mercibeaucouppour votre réponseet excusez-moi égalementpourma réponsetardive.J'aiessayé comme ça.`$ content=get_the_content ();$ content=preg_replace ('/(<) ([galerie]) (\ w +) ([^>] *>)/',",$ content);$ content=apply_filters ('the_content',$ content);$ content=str_replace (']]>',']] >',$ content);echo $ content; `Mais celan'apasfonctionné.Aucuneimagen'est suppriméeet jene vois aucuntexte quej'attendsThank you so much for your reply and also apologize for my late response. I tried like this. `$content = get_the_content(); $content = preg_replace('/(<)([gallery])(\w+)([^>]*>)/', ”, $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;` But it didn't work. No images are removed and can't see any text that I expect.
- 1
- 2013-02-24
- Thiha Maung
-
- 2017-11-16
Le code suivantfonctionneparfaitementpourmoi.Ajoutez simplement ceci à votrefichier
functions.php
dans votrethème:// Hook : to get content without images add_filter('the_content', 'wpse_get_content_without_images'); function wpse_get_content_without_images() { $content = get_the_content(); $content = preg_replace( '/<img[^>]+./', '', $content ); echo $content; }
Ensuite,récupérez le contenu de votremessage de lamanière standarden utilisant
echo the_content();
.The following code works perfectly for me. Just add this to your
functions.php
file in your theme:// Hook : to get content without images add_filter('the_content', 'wpse_get_content_without_images'); function wpse_get_content_without_images() { $content = get_the_content(); $content = preg_replace( '/<img[^>]+./', '', $content ); echo $content; }
Then, get your post content the standard way using
echo the_content();
. -
- 2018-06-10
J'ai combiné les résultats des autres réponses sur cepostpour supprimer lesimageset l'audio,etc.,maisen conservant leformatage.En obtenant d'abord le contenu avec
get_the_content
,puisen lepassant àtravers lefiltre "the_content"pour ajouter unemiseen forme,etc.,puisen utilisant lesstrip_tags
dephppourn'autoriser qu'unnombre limité debalises.strip_tags(apply_filters('the_content',get_the_content()),"<p><a><br><b><u><i><strong><span><div>");
I combined results from the other answers on this post to strip images and audio etc, but keeping the formatting. By first getting the content with
get_the_content
, then passing it through the "the_content" filter to add formatting etc, and then using php'sstrip_tags
to only allow a limited number of tags.strip_tags(apply_filters('the_content',get_the_content()),"<p><a><br><b><u><i><strong><span><div>");
-
- 2019-01-02
Voici le codepour supprimer le contenu de lagalerieet n'avoir que du contenu.
$content = get_the_content(); $content = preg_replace('/\[gallery.*ids=.(.*).\]/', "", $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Here is the code to remove gallery content and to have only content.
$content = get_the_content(); $content = preg_replace('/\[gallery.*ids=.(.*).\]/', "", $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
-
N'est-cepasexactement ce que certaines des autres réponses ont dit avecmoins d'explications?Isn't that exactly what some of the other answers have said only with less explanation?
- 0
- 2019-09-04
- Matthew Brown aka Lord Matt
Je développe actuellement un site Web avec WordPress 3.5et je dois récupérer le texte dumessage (uniquement dutexte,sansinclure lesimages) sur la page d'archive .Jepeux le récupérer avec laméthode
wp_excerpt()
sans aucunproblème.Mais leprincipalproblèmepourmoiest queje nepeuxpas obtenir lamiseen pageexacte dutexte.Laméthodewp_excerpt()
renvoie dutexte quiignoretous lesespaces supplémentaireset les sauts de ligne.Que devrais-jefaire?Jepense quej'obtiendrai Publier uniquement letexte avec unemiseen pageexacte sije peux récupérer àpartir de laméthodewp_content()
.Merci d'avancepour votre aide!