WordPress教程

限制WordPress文章标题长度字数多种方法

阿里云

很多小伙伴发布的 WordPress 文章标题非常长,如果你的 WordPress 文章标题太长了会导致主题异常影响美观,对于很多小伙伴来说 WordPress 具有着非常折腾的心,今天就有朋友问我如何限制 WordPress 文章标题长度字数方法,这里我罗列了几种方法,包括前端类型限制 WordPress 文章标题长度字数方法,与后端编辑器限制 WordPress 文章标题长度字数方法。

WordPress 自带的函数是直接输出文章标题长度的,标题太长了就会自动换行,解决办法是使用 mbstring 函数库来解决,这样就可以指定具体标题字数。

也想出现在这里?联系我们
创客主机

前端限制文章标题字数

  1. function short_title() {
  2. $mytitleorig = get_the_title();
  3. $title = HTMLspecialchars($mytitleorig, ENT_QUOTES, "UTF-8"); 
  4.  
  5. $limit = "15"; //显示的字数,可根据需要调整
  6. $pad="";
  7.  
  8. if(strlen($title) >= ($limit+3)) {
  9. $title = mb_substr($title, 0, $limit) . $pad; }
  10. echo $title;
  11. }

调用:short_title(),或者使用以下更简单方法

  1. wp_trim_words(get_the_titlee(),5; 
  2. echo wp_trim_words( get_the_title(), 10, '...' );
  3. echo mb_strimwidth(htmlspecialchars_decode(get_the_title()), 0, 50, '...');

现在大部分的 PHP 服务器都支持了 MB 库(mbstring 库 全称是 Multi-Byte String 即各种语言都有自己的编码,他们的字节数是不一样的,目前 php 内部的编码只支持 ISO-8859-*, EUC-JP, UTF-8 其他的编码的语言是没办法在 php 程序上正确显示的。解决的方法就是通过 php 的 mbstring 函数库来解决),所以我们可以放心的使用这个用于控制字符串长度的函数。

备注:如何出现转移乱码等可以采用这个 the_title_attribute()替换 get_the_title()

后端限制 WordPress 文章标题

  1. //限制文章标题输入字数
  2. function title_count_js(){
  3. 	echo '<script>jQuery(document).ready(function(){
  4. 	jQuery("#titlewrap").after("<div><small>标题字数: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"title_counter\" readonly=\"\" style=\"background:#fff;\"> <small>最大长度不得超过 46 个字</small></div>");
  5. 	jQuery("#title_counter").val(jQuery("#title").val().length);
  6. 	jQuery("#title").keyup( function() {
  7. 	jQuery("#title_counter").val(jQuery("#title").val().length);
  8. 	});
  9. 	jQuery("#titlewrap #title").keyup( function() {
  10. 	var $this = jQuery(this);
  11. 	if($this.val().length > 46)
  12. 	$this.val($this.val().substr(0, 46));
  13. 	});
  14. });</script>';
  15. }
  16. add_action( 'admin_head-post.php', 'title_count_js');
  17. add_action( 'admin_head-post-new.php', 'title_count_js');
  18. //其它
  19. add_filter( 'the_title', 'wpse_75691_trim_words' );
  20. function wpse_75691_trim_words( $title )
  21. {
  22.     // limit to ten words
  23.     return wp_trim_words( $title, 10, '' );
  24. }
  25. add_filter( 'the_title', 'wpse_75691_trim_words_by_post_type', 10, 2 );
  26. function wpse_75691_trim_words_by_post_type( $title, $post_id )
  27. {
  28.  
  29.     $post_type = get_post_type( $post_id );
  30.  
  31.     if ( 'product' !== $post_type )
  32.         return $title;
  33.  
  34.     // limit to ten words
  35.     return wp_trim_words( $title, 10, '' );
  36. }
  37. function limit_word_count($title) {
  38.     $len = 5; //change this to the number of words
  39.     if (str_word_count($title) > $len) {
  40.         $keys = array_keys(str_word_count($title, 2));
  41.         $title = substr($title, 0, $keys[$len]);
  42.     }
  43.     return $title;
  44. }
  45. add_filter('the_title', 'limit_word_count');

限制 WordPress 文章标题长度字数多种方法

已有 290 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!