flexpress / component-post-type

此包最新版本(v1.0.0)没有可用的许可信息。

WordPress文章类型助手

v1.0.0 2014-08-13 14:40 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:42:18 UTC


README

使用Pimple安装

PostType组件使用了两个类

  • AbstractPostType,通过扩展它来创建PostType。
  • PostTypeHelper,它为您挂钩到所有操作并注册文章类型。

让我们为这两个类创建一个pimple配置

$pimple["documentPostType"] = function () {
  return new Document();
};

$pimple['PostTypeHelper'] = function ($c) {
    return new PostTypeHelper($c['objectStorage'], array(
        $c["documentPostType"]
    ));
};
  • 注意,$c['objectStorage'] 是一个 SPLObjectStorage

创建一个具体的PostType类

创建一个实现AbstractPostType类并实现getName()方法的具体类。

class DocumentType extends AbstractPostType {

    public function getName()
    {
      return "document";
    }
    
}

上述示例是必须实现的最小示例,接下来的示例是实现所有可用方法的另一个极端。

class Document extends AbstractPostType {

  public function getSingularName()
  {
    return "Doc";
  }
  
  public function getPluralName()
  {
    return "Docs";
  }
  
  public function getArgs()
  {
    $args = parent::getArgs();
    $args['supports'] = array("title", "editor");
    return $args;
  }
  
  protected function getLabels()
  {
    $labels = parent::getLabels();
    $labels['menu_name'] = 'Docs';
    return $labels;
  }
    
  public function getName()
  {
    return "document";
  }

}

公共方法

  • getSingularName() - 返回文章类型的单数名称。
  • getPluralName() - 返回文章类型的复数名称。
  • getArgs() - 返回参数数组。
  • getLabels() - 返回标签数组。
  • getName() - 返回文章类型名称。

PostTypeHelper使用方法

配置好pimple后,您可以使用PostTypeHelper如下

$helper = $pimple['postTypeHelper'];
$helper->registerPostTypes();

就是这样,助手将添加所有需要的钩子和注册您提供的所有文章类型。

公共方法

  • registerPostTypes() - 注册提供的文章类型。