开发者

Wordpress how to set the post excerpt length to 100 characters and 200 characters

开发者 https://www.devze.com 2023-01-10 22:43 出处:网络
Example I want 开发者_StackOverflowuse the_excerpt in two locations. On functions.php I add function new_excerpt_length($length) {

Example I want 开发者_StackOverflowuse the_excerpt in two locations. On functions.php I add

function new_excerpt_length($length) {
    return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

Question.

How to make another function to accept another the_excerpt will be 200 characters?

Or another solutions.

How to make something like this.. Dynamic the_excerpt()

  1. Option 1 = the_excerpt() limit to 100 chars
  2. Option 2 = the_excerpt() limit to 200 chars
  3. Option 3 = the_excerpt() limit to 300 chars


Have you read through the WordPress documentation for the_excerpt()

http://codex.wordpress.org/Function_Reference/the_excerpt

It appears that you can only override the the excerpt_length() with new_excerpt_length() via filters as you mentioned.

So perhaps you can modify existing the_excerpt function to take a parameter $length, where the default value = null. If you are familiar with PHP you know that you don't have to pass any parameters to the_excerpt(), in which case $length will default to null and function as if $length was never passed to it, which means all current usages of the_excerpt() will continue working as normal. and if you want print your excerpt at a different length then call the_excerpt(someOtherLength);

It will look something like this.

function the_excerpt($length=null) {
     if($length != null) {
         // ignore default excerpt length with $length 
         // print the excerpt to $length 
     } else {
    // prints the excerpt to standard length specified by excerpt_length()
     }
}

Let me know if you have any problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消