从类中返回get_the_excerpt()以获取多个wordpress摘录

时间:2010-12-07 15:08:23

标签: php oop wordpress

我一直在修改我在Stack Overflow上发现的一个类,用于改变Wordpress的摘录长度。它一直是一只熊(因为我是OOP的新手),但它最终确实有效并接受第二个参数来过滤更多的链接。我想做的是,目前输出的是'the_excerpt',它会在调用函数“my_excerpt()”时立即回显。我想添加一个名为“get_my_excerpt”的函数来返回值。我知道get_the_excerpt()正是这样做的,但我似乎无法让它在这个类中工作。

  /* Class that enables excerpt length parameter */
/* called via my_excerpt('length') */

class Excerpt {

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

   // Default more (by WordPress)
  public static $more = "[...]";

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

    // So you can call: my_excerpt('short');
    public static $more_types = array(
      'none' => "",
      'regular' => "[...]",
      'ellipse' => "...",
      'permalink' => 'skip',
    );




  /**
   * 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 filter($new_length = 55, $new_more="[...]", $echo=TRUE) {
    Excerpt::$length = $new_length;
    Excerpt::$more = $new_more;

    add_filter('excerpt_length', 'Excerpt::new_length',98);
    add_filter('excerpt_more', 'Excerpt::new_more',99);

    return 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;
  }

   // Tells WP the new more
  public static function new_more() {

  $db = new ReadMore;

    if( isset(Excerpt::$more_types[Excerpt::$more]) AND ( (Excerpt::$more_types[Excerpt::$more]) != "skip" ) )
      return Excerpt::$more_types[Excerpt::$more];
    elseif( isset(Excerpt::$more_types[Excerpt::$more]) AND ( (Excerpt::$more_types[Excerpt::$more]) == "skip" ) )
      return $db->readmore();
    else
      return Excerpt::$more;
  } 

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



}

// An alias to the class
function get_my_excerpt($length = 55, $more="[...]") {
  return Excerpt::filter($length, $more);
}

// An alias to the class
function my_excerpt($length = 55, $more="[...]") {
  echo get_my_excerpt($length, $more);
}


class ReadMore {
  private $title;
  private $permalink;
  private $more;


  public function __construct () {
    //$this->title = get_the_title();
    //$this->permalink = get_permalink();
    $temp = "..." . '<a class="readmore" title="'. _('Permalink to').get_the_title() . '" href=" ' . get_permalink() . '">'._('Read the rest').'</a>';
    $this->more = $temp;

  }
  public function readmore() {    
    return $this->more;
  }
}

1 个答案:

答案 0 :(得分:1)

如果您不想重写或复制粘贴一些代码,我认为这段代码可以提供帮助,代码不优雅但有效,只需添加此功能:

function get_my_excerpt($length = 55, $more="[...]") {
  ob_start();
  Excerpt::filter($length, $more);
  $my_excerpt = ob_get_contents();
  ob_end_clean();
}

我想更好的解决方法是重写一些代码,例如,在静态函数output()中使用get_the_excerpt()而不是the_excerpt(),在Excerpt :: filter函数中添加相应的返回值,在my_excerpt函数中添加一个echo,最后添加这个函数:

function get_my_excerpt($length = 55, $more="[...]") {
  return Excerpt::filter($length, $more);
}