Summary of WordPress articles

Time:2020-1-11

In fact, I don’t like the display of summary very much, but this method is more convenient to use.
WordPress has a summary function. It’s a good habit to fill in some summaries when writing an article. I set the summary for the search engine.
However, not many people fill in this abstract when they publish an article, so I have been wondering whether the abstract mode is preferred in theme design.
Most of the codes that use abstracts on the Internet are like this

Copy code

The code is as follows:

<?php if(!is_single()) {
the_excerpt();
} else {
the_content(__(‘(more…)’));
} ?>

The first choice is a summary of the display. If there is no summary, all the articles will be displayed. It is certain that the whole article is not helpful. In case of a long article, how many articles can be placed on the front page?
Today, I saw a very good article that solved this problem. The article thought of what I wanted. It was very good. A more perfect scheme for Abstracts (truncation) of WordPress articles
First, the summary will be displayed. If there is no summary, it will be displayed with the more tag. If there is no more tag, it will be displayed with how many words the article intercepts. If the host does not support the MB ﹣ stringwidth function truncation, the full text will be displayed.
What do you think of this display method? If you think it’s OK, my new theme will use this as the summary of the homepage article.

Copy code

The code is as follows:

< PHP / / this is to display the whole content directly in a single article (or page), instead of using the following judgment
if(is_singular()){the_content();}else{
//Define two$
$pc=$post->post_content;
$st=strip_tags(apply_filters(‘the_content’,$pc));
//Determine if there is a built-in summary
if(has_excerpt())
the_excerpt();
/*Use regular expressions to try to match the more tag,
And judge that if there is a more tag, or the length of the body content is less than 300, it will be displayed directly in the form of a more tag,
That is to say, if there is more, it will show more; if there is no more and the length is less than 300, it will show the full text directly
I used mb_strwidth to determine the length
*/
elseif(preg_match(‘/<!–more.*?–>/’,$pc) || mb_strwidth($st)<300)
the_content(‘Read more &raquo;’);
//Otherwise… Below
Else if (function_exists (‘mb_stringwidth ‘)) / / judge whether the mb_stringwidth function is supported
Echo ‘< p >’ / / mb_stringwidth is truncated to get plain text, so I wrapped a layer of P
. mb_stringwidth ($st, 0300, ‘…’) / / truncate the text content. Intercept 300 length from position 0. Replace the excess with dots
.'</p><p><a title= “/ / to add a” more tag “exactly the same as STEP 1 to the truncated content.
. the “title (” read on: “,” “, false) / / note that this is a different function than step 1 to get the title
.'” rel=”nofollow” href=”‘
.post_permalink()
.'”>Read more &raquo;</a></p>’;
Else the “content(); / / there is no choice at last
}?>