Auszug in Zeichen
4 Antworten
- Stimmen
-
- 2012-10-16
Ich habe diesen Codein einemmeiner letzten Projekte verwendet:
function ng_get_excerpt( $count ){ $permalink = get_permalink( $post->ID ); $excerpt = get_the_content(); $excerpt = strip_tags( $excerpt ); $excerpt = mb_substr( $excerpt, 0, $count ); $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) ); $excerpt = rtrim( $excerpt, ",.;:- _!$&#" ); $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;"> (...)</a>'; return $excerpt; }
Ich habees von hierbekommen:
http://wordpress.org/support/topic/limit -auszug-Länge-nach-Zeichen
https://stackoverflow.com/questions/10923955/make-function-that-limit-text-not-show-last-Satzzeichen
Es hat den Vorteil,dass keine Interpunktion am Ende zulässigist undmit dem letzten vollständigen Wortendet
Verwenden Sie die Filter,wie von @medhamza7 oder @bainternet oder @fuxia ist vorzuziehen.
I used this code in one of my last projects:
function ng_get_excerpt( $count ){ $permalink = get_permalink( $post->ID ); $excerpt = get_the_content(); $excerpt = strip_tags( $excerpt ); $excerpt = mb_substr( $excerpt, 0, $count ); $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) ); $excerpt = rtrim( $excerpt, ",.;:- _!$&#" ); $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;"> (...)</a>'; return $excerpt; }
I got it from here:
http://wordpress.org/support/topic/limit-excerpt-length-by-characters
It has the advantage of not allowing punctuation on the end and ending with the last complete word
Using the filters as suggested by @medhamza7 or @bainternet or @fuxia is preferable.
-
Vielen Dankfür diesen Rat,ein effizienterfürmichisteinfach: "echo substr (get_the_excerpt (),0,30);"entnommen aus Ihren Links.Very thanks for this advice, efficient one for me is just simple: "echo substr(get_the_excerpt(), 0,30);" taken from Your links.
- 0
- 2012-10-16
- Marcin
-
@Marcin [`substr ()` wirdbrechen] (http://wpengineer.com/2410/dont-use-strlen/).Verwenden Sieesniemalsfür UTF-8-codierte Daten.@Marcin [`substr()` will break](http://wpengineer.com/2410/dont-use-strlen/). Never use it on UTF-8 encoded data.
- 0
- 2012-10-16
- fuxia
-
- 2012-10-16
Verwenden Sie die Funktion
utf8_truncate()
aus dieser Antwort und kämpfen Sie sich durchwp_trim_excerpt()
.Beispielcode,nichtgetestet:
add_filter( 'excerpt_more', 'wpse_69436_excerpt_more' ); function wpse_69436_excerpt_more( $more ) { add_filter( 'wp_trim_excerpt', 'wpse_69436_trim_excerpt' ); // we remove the more text here return ''; } function wpse_69436_trim_excerpt( $excerpt ) { return utf8_truncate( $excerpt, 300 ); }
Use the function
utf8_truncate()
from this answer and fight your way throughwp_trim_excerpt()
.Sample code, not tested:
add_filter( 'excerpt_more', 'wpse_69436_excerpt_more' ); function wpse_69436_excerpt_more( $more ) { add_filter( 'wp_trim_excerpt', 'wpse_69436_trim_excerpt' ); // we remove the more text here return ''; } function wpse_69436_trim_excerpt( $excerpt ) { return utf8_truncate( $excerpt, 300 ); }
-
- 2012-10-16
WordPress hateinen Filterfür das,wasbequemerweiseexcerpt_length heißt,undes akzeptierteine Reihe von Zeichen,also:
function custom_excerpt_length( $length ) { return 50; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
Ändern Sie 50 auf dasgewünschte Limit.
Updateper @toscho-Kommentar:
Dasist die obige Lösungfür Wörter undnichtfür Zeichen. Hieristeine kurze:
add_filter('the_excerpt','excerpt_char_limit'); function excerpt_char_limit($e){ return substr($e,0,50); }
WordPress has a filter for that which is conveniently named excerpt_length and it accepts a number of chars so:
function custom_excerpt_length( $length ) { return 50; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
change 50 to whatever limit you want.
Update per @toscho comment:
that is the solution above is for words as well and not for chars so here is a quick one:
add_filter('the_excerpt','excerpt_char_limit'); function excerpt_char_limit($e){ return substr($e,0,50); }
-
Dasistnichtfür Charaktere,sondernfür Wörter.That’s not for characters, it is for words.
- 1
- 2012-10-16
- fuxia
-
@toscho Mein schlechtes!aktualisiertmit einereinfachen Lösung@toscho My bad! updated with a simple solution
- 0
- 2012-10-16
- Bainternet
-
Wiefunktioniertes?Ich habees als "echo custom_excerpt_length ()" angegeben,aberesfunktioniertnicht.How does it works? i put it as "echo custom_excerpt_length(); " but it doesn't work.
- 0
- 2012-10-16
- Marcin
-
Danke,jetztfunktioniertes,aber wie kannmaneinige Auszügefür die Titelseite angeben?Thanks, now it works, but how to give some excerpts lengths for front page?
- 0
- 2012-10-16
- Marcin
-
@Bainternet - Ichgab: "add_filter ('the_excerpt2','excerpt_char_limit2'); Funktionexcerpt_char_limit2 ($e) { return substr ($e,0,10); } "aberesfunktioniertnicht.@Bainternet - I gave: "add_filter('the_excerpt2','excerpt_char_limit2'); function excerpt_char_limit2($e){ return substr($e,0,10); }" but it doesn't work.
- 0
- 2012-10-16
- Marcin
-
- 2017-11-06
Füreine bessere Möglichkeit können Sie den Filter
get_the_excerpt
verwenden:function get_excerpt($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; } add_filter('get_the_excerpt',"get_excerpt");
Ändern Sie das
$limit=140
in diegewünschte Anzahl von Zeichen. Auch wenn Sie auf andere Weisemöchten:add_filter('get_the_excerpt',function ($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; });
Dadurch werden Konflikte wie der vorhandene Name der Funktion
get_excerpt
vermieden.For a better way, you can use the
get_the_excerpt
filter:function get_excerpt($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; } add_filter('get_the_excerpt',"get_excerpt");
Change the
$limit=140
to the number of characters you want. Also if you want in different way:add_filter('get_the_excerpt',function ($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; });
That will make avoid any conflict like existing name of function
get_excerpt
.-
@Nicolai Ich denke,wir sollten die Verwendung von `mb_substr` vermeiden,daeinige PHP-Konfigurationen standardmäßig keine MB-Funktionen aktivieren:/@Nicolai i think we should avoid using `mb_substr` cause some php config by default doesn't activated mb functions :/
- 0
- 2018-02-26
- med amine hamza
-
Wie hiergelesen (https://wordpress.stackexchange.com/a/11089/22534),hat WPeinen Fallback dafür.As read [here](https://wordpress.stackexchange.com/a/11089/22534), WP has a fallback for it.
- 0
- 2018-02-26
- Nicolai
Ich habe Codein functions.php:
aberichmuss den Auszugin der Anzahl der Zeichenbegrenzen, Könnten Siemir dabei helfen?