Home  >  Article  >  Backend Development  >  Usage example of wordpress's excerpt() function

Usage example of wordpress's excerpt() function

不言
不言forward
2019-04-01 09:36:183660browse

The content of this article is about the usage examples of WordPress’s excerpt() function. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Problem: In the single page in wordpres, it refers to 15343524653da45df253268258fb1516, but what is displayed on the page is the content of the article

Reason: the_excerpt (); When excerpt has no content, the content of the article will be intercepted.

Wordpress built-in function the_excerpt() is a frequently used function. It is used to obtain the summary of the current article. It ends with [...]. If there is no editing content summary field in the article, it defaults to Intercept the first 55 words of the article. By default, HTML tags and graphics are removed from the intercepted fields, and they must be used within a loop (! Sometimes it can be displayed without looping, but in some cases it will be confusing).

Usage: The method of using the_excerpt() function is also very simple. The usage is as follows:

This tag does not have any parameters and can be used directly. However, the default settings of the function are sometimes not satisfactory. User needs. For example, domestic users are not used to ending with [...]. In addition, the first 55 characters are sometimes too few. Also, can we customize the end of the article summary and add more? As for links, these customizations only need to add the corresponding code to the theme functions.php file.

Control the number of words in the summary:

/*控制摘要字数*/
function new_excerpt_length($length) {
return 150;
}
add_filter("excerpt_length", "new_excerpt_length");

return 150 is the returned character, two characters and one Chinese character. This can be set according to your own needs.

Change the default display style at the end of the summary:

function new_excerpt_more($excerpt) {
return str_replace("[...]", "...", $excerpt);
}
add_filter("wp_trim_excerpt", "new_excerpt_more")

the_excerpt() function ends with [...] by default. Here we use PHP’s replacement function str_replace Replace it with... or change it to the symbol you want.

Add a custom ending:

function new_excerpt_more($more) {
global $post;
return " <a href="". get_permalink($post->ID) . "">阅读更多</a>";
}
add_filter("excerpt_more", "new_excerpt_more");

Add a read more link at the end of the article summary, which looks more in line with the user's reading habits. Read more can be changed to your own desired content.

Just add the above code to the theme functions.php file.

[Recommended course: PHP video tutorial]

The above is the detailed content of Usage example of wordpress's excerpt() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete