4/1/16

Những cách để rút gọn tiều đề bài viết trong Wrodpress

Làm thế nào để rút gọn tiều đề bài viết trong Wrodpress một cách dễ dàng? Hôm này mình sẽ giúp các bạn trả lời những câu hỏi này.

Cách 1: Đặt đoạn code sau vào bất kỳ đâu mà bạn muốn hiển thị title cần rút gọn.

<?php if (strlen(the_title('','',FALSE)) > 30) { //Character length          $title_short = substr(the_title('','',FALSE), 0, 30); // Character length          preg_match('/^(.*)\s/s', $title_short, $matches);      if ($matches[1]) $title_short = $matches[1];          $title_short = $title_short.' ...'; // Ellipsis      } else {          $title_short = the_title('','',FALSE);      } ?><?php echo $title_short ?> 

Ví dụ:
 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <?php if (strlen(the_title('','',FALSE)) > 30) {           $title_short = substr(the_title('','',FALSE), 0, 30);           preg_match('/^(.*)\s/s', $title_short, $matches);       if ($matches[1]) $title_short = $matches[1];           $title_short = $title_short.' read more >';       } else {           $title_short = the_title('','',FALSE);       } ?>
 <h1><?php echo $title_short ?></h1>
 <div class="the-post"><?php the_content(); ?></div>
<?php endwhile; endif; ?>
30 là con số bạn muốn thay đổi độ dài hiển thị 

Cách 2: Ở cách này các bạn cần đặt code sau vào functions.php

function ShortenText($text) { // Function name ShortenText  $chars_limit = 100; // Character length  $chars_text = strlen($text);  $text = $text." ";  $text = substr($text,0,$chars_limit);  $text = substr($text,0,strrpos($text,' '));
  if ($chars_text > $chars_limit)     { $text = $text."..."; } // Ellipsis     return $text;}
Muốn hiển thị ở đâu chỉ cần gọi đoạn code sau là được:
<?php echo ShortenText(get_the_title()); ?>
Ví dụ: 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h1><?php echo ShortenText(get_the_title()); ?></h1>
 <div class="post-title"><?php the_content(); ?></div>
<?php endwhile; endif; ?>

Cách 3: Tương tự như cách 2 các bạn cần đặt code sau vào functions.php

function short_title($after = '', $length) {   $mytitle = explode(' ', get_the_title(), $length);   if (count($mytitle)>=$length) {       array_pop($mytitle);       $mytitle = implode(" ",$mytitle). $after;   } else {       $mytitle = implode(" ",$mytitle);   }       return $mytitle;}
Đặt code này vào nơi các bạn muốn hiển thị title:
<?php // short_title('ellipsis', 'wordlength') echo short_title('...', 10); ?>
Ví dụ:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h1><?php echo short_title('read-more >', 10); ?></h1>
 <div class="post-title"><?php the_content(); ?></div>
<?php endwhile; endif; ?>

Cách 4: Tương tự như cách 2 và 3 các bạn cần đặt code sau vào functions.php

function short_title($before = '', $after = '', $echo = true,  $length = false) { $title = get_the_title(); if ( $length &amp;amp;&amp;amp; is_numeric($length) ) {      $title = substr( $title, 0, $length ); } if ( strlen($title)> 0 ) {      $title = apply_filters('short_title', $before . $title .  $after, $before, $after); if ( $echo )      echo $title; else      return $title; }}Đặt code sau vào nơi muốn hiển thị<?php // short_title('BeforeText'. 'ellipsis', 'true', 'wordlength') short_title('**','...',true, '20'); ?>
Ví dụ:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h1><?php short_title('**','read more >',true, '20'); ?></h1>
 <div class="post-title"><?php the_content(); ?></div>
<?php endwhile; endif; ?>

Cách 5: Tương tự như cách 2, 3 và 4 các bạn cần đặt code sau vào functions.php

function the_title_limit($length, $replacer = '...') { $string = the_title('','',FALSE); if(strlen($string) > $length) $string = (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer; echo $string;}
Đặt code sau vào nơi muốn hiển thị
<?php the_title_limit( 30, '...'); ?>
Ví dụ:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h1><?php the_title_limit( 30, 'read more >'); ?></h1>
 <div class="post-title"><?php the_content(); ?></div>
<?php endwhile; endif; ?>
Chúc các bạn thành công !

0 nhận xét: