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

Usage example of wordpress's excerpt() function

不言
Release: 2023-04-05 18:36:02
forward
3741 people have browsed it

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 , 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");
Copy after login

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")
Copy after login

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");
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template