logo помогаю бизнесу зарабатывать больше.
Связаться
Введите название услуги:

Длина Expert

2 июня 2022 в 15:16 Блог веб-разработчика

Код

class Excerpt {

  // Default length (by WordPress)
  public static $length = 55;

  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 25,
      'regular' => 55,
      'long' => 100
    );

  /**
   * Sets the length for the excerpt,
   * then it adds the WP filter
   * And automatically calls the_excerpt();
   *
   * @param string $new_length 
   * @return void
   * @author Baylor Rae'
   */
  public static function length($new_length = 55) {
    Excerpt::$length = $new_length;

    add_filter('excerpt_length', 'Excerpt::new_length');

    Excerpt::output();
  }

  // Tells WP the new length
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
    else
      return Excerpt::$length;
  }

  // Echoes out the excerpt
  public static function output() {
    the_excerpt();
  }

}

// An alias to the class
function my_excerpt($length = 55) {
  Excerpt::length($length);
}

Варианты использования

my_excerpt('short'); // calls the defined short excerpt length
my_excerpt(40); // 40 chars

 

Похожие статьи

Оставить заявку
По любым вопросам пишите мне в мессенджеры .
Написать в WhatsApp
+7 (925) 444-48-89
Написать в Telegram
@max_webs
Ваша цена
Предложите свою цена за товар или услугу. Пишите мне в мессенджеры или оставьте заявку в данной форме.
Написать в WhatsApp
+7 (925) 444-48-89
Написать в Telegram
@max_webs
[contact-form-7 id="22192" title="Универсальная форма"]
[contact-form-7 id="114828" title="Рассчитать стоимость"]