Introduction to functions intercepted by wordPress in Chinese

Source: Internet
Author: User
Tags ord

Chinese truncation function

The code is as follows: Copy code

Function utf8_trim ($ str)
{
$ Len = strlen ($ str );
For ($ I = strlen ($ str)-1; $ I >=0; $ I-= 1 ){
$ Hex. = ''. ord ($ str [$ I]);
$ Ch = ord ($ str [$ I]);
If ($ ch & 128) = 0) return (substr ($ str, 0, $ I ));
If ($ ch & 192) = 192) return (substr ($ str, 0, $ I ));
}
Return ($ str. $ hex );
}
Function mul_excerpt ($ excerpt, $ excerpt_length ){
$ Myexcerpt = substr ($ excerpt, 0, $ excerpt_length );
Return utf8_trim ($ myexcerpt ).'... ';
}

Alternatively

// Automatically truncation without garbled characters
// Jieduan luanma
/////////////////
Function Limit_Char ($ max_char = 200, $ more_text = '...', $ more_link_text = '', $ limit_type = 'content '){
If ($ limit_type = 'TITLE') {$ limiter = get_the_title ();}
Else {$ limiter = get_the_content ();}
$ Limiter = apply_filters ('The _ content', $ limiter );
$ Limiter = strip_tags (str_replace (']>', ']>', $ limiter ));
If (strlen ($ limiter)> $ max_char ){
$ Limiter = substr ($ limiter, 0, $ max_char + 1 );
$ Limiter = utf8_conver ($ limiter );
Echo $ limiter;
Echo $ more_text;
If ($ more_link_text! = ''){
Echo ''. $ more_link_text .'';
        }
} Else {
Echo $ limiter;
    }
}

Function utf8_conver ($ str ){
$ Len = strlen ($ str );
For ($ I = strlen ($ str)-1; $ I >=0; $ I-= 1 ){
$ Hex. = ''. ord ($ str [$ I]);
$ Ch = ord ($ str [$ I]);
If ($ ch & 128) = 0) return (substr ($ str, 0, $ I ));
If ($ ch & 192) = 192) return (substr ($ str, 0, $ I ));
        }
Return ($ str. $ hex );
}

Extract Chinese titles

The code is as follows: Copy code

Echo '</p> <p> '. mb_substr (get_the_title (), 0, 18, "utf8 "). "... ". '</p> </li> ';

This will lead to chips.

I added the Chinese screenshot code above to functions. php.


Then, add get_short_title () to the position where the title is to be captured to achieve the goal. The detailed code is as follows:

The code is as follows: Copy code

Echo '<li> <a href = "'. get_permalink (). '"> ';
Get_short_title (18 );
Echo '</a> <data>'. get_the_time ('N months j ').' </data> </li> ';

Of course, you can call it in other places like this:

The code is as follows: Copy code

<? Php get_short_title ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.