potterywp / potter
一套帮助您更容易制作WordPress网站的工具。
Requires
- php: >=5.4.0
- anahkiasen/html-object: ~1.4
- illuminate/support: ~5
- rilwis/meta-box: ~4.4
- valendesigns/option-tree: ~2.4
README
一套帮助您更容易制作WordPress网站的工具。
Potter还处于测试阶段
安装
在其composer.json
"require": {
"potterywp/potter": "1.*"
},
在其functions.php
require_once "vendor/autoload.php";
特性
- Post/Type - 轻松灵活地创建自定义帖子类型
- ThemeOptions - 简化主题选项的创建。Potter使用option-tree包装器,进一步优化了开发流程。
- 特性 - Potter提供一系列辅助器,可轻松将功能集成到您的WordPress项目中,如轻松在页面中包含CSS和JavaScript。
特性
要使用特性,请将以下代码插入到您的functions.php
中,在require_once "vendor/autoload.php";
之后。使用Features函数的优势在于组织优化,因为您只需几行代码即可获得WP的多个功能,而您可能需要将它们分开到多个文件中才能保持组织。Potter只在实际需要时加载命令。
use Potter\Potter; $features = Potter::features();
菜单
向您的主题添加菜单
// $features->addMenu($location, $description); $features->addMenu('main', 'Menu Principal');
主题支持
// $features->addThemeSupport($feature, $arguments = array()); $features->addThemeSupport('post-thumbnails'); $features->addThemeSupport('post-formats');
帖子类型支持
// $features->addPostTypeSupport($post_type, $feature) $features->addPostTypeSupport('page', 'excerpt');
图片大小
// $features->addImageSize($name, $width = 0, $height = 0, $crop = false); $features->addImageSize('thumb-home', 300, 300, true); $features->addImageSize('thumb-page', 248, 888, true); $features->addImageSize('thumb-contact', 460, 400, false);
资产
轻松添加和组织CSS和JavaScript文件。
CSS
// $features->addCss($handle, $src = false, $deps = array(), $ver = null, $media = 'all'); $features->addCss('gfonts', 'http://fonts.googleapis.com/css?family=Rosario:400,700'); $features->addCss('main', 'assets/pub/css/main.css');
JS
// $features->addJs($handle, $src, $deps = array(), $ver = null, $in_footer = false); // $features->addJsToHead($handle, $src, $deps = array(), $ver = null); // $features->addJsToFooter($handle, $src, $deps = array(), $ver = null); $features->addJsToHead('modernizr', 'assets/pub/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js'); $features->addJsToFooter('app', 'assets/pub/js/app.js');
jQuery CDN
定义您正在使用的jQuery版本。Potter将自动调整所有必要的配置。
// $features->setJqueryCDNSupport($version, $fallback = null, $migrate = null, $in_footer = false); $features->setJqueryCDNSupport('2.1.1', 'assets/pub/js/vendor/jquery-2.1.1.min.js', 'assets/pub/js/vendor/jquery-migrate-1.2.1.min.js');
除了定义您想要的jQuery版本外,您还可以定义回退(如果CDN无法加载)并定义jQuery迁移。
Google Analytics
只需一行代码即可将Google Analytics跟踪代码添加到主题中。
// $features->setGoogleAnalytcsID($id); $features->setGoogleAnalytcsID('A1-XXXXXX');
登录Logo
更改WordPress的登录Logo(d+不?)
// $features->setLoginLogo($logo, $style = array()); $features->setLoginLogo('assets/pub/imgs/login-logo.png', array('width'=>'150px'));
主题选项
使用WordPress构建网站非常好,但在某些时候,我们需要通过最终用户环境(客户)使某些资源更加灵活。为此,我们使用诸如Theme Options之类的资源。
实现Theme Options有多种方式,有的简单、有的强大。插件option-tree是一个很好的选择,它简单灵活,但没有一个让所有人都满意的开发界面。
因此,Potter提供了一组API包装器,可以极大地简化使用option-tree进行Theme Options开发的工作。
创建您的ThemeOptions
- 在您的主题根目录
/wp-content/themes/meutema/
中创建一个名为/app
的文件夹 - 在
app
文件夹中创建一个名为ThemeOptions.php
的文件 - 在
ThemeOptions.php
中放置以下代码
<?php use Potter\Theme\Options; class ThemeOptions extends Options { protected $page_title = 'Opções do Tema'; protected $menu_title = 'Opções do Tema'; protected $settings_id = 'my_theme_options_id'; public function doRegister() { } }
当Potter初始化其配置时,它会自动创建一个
ThemeOptions
实例,因此您只需创建文件并放置配置即可,无需做其他任何事情。
附加设置
您还有更多选项可供选择,以更好地自定义您的ThemeOptions
class ThemeOptions extends Options { protected $page_title = 'Theme Options'; protected $menu_title = 'Theme Options'; protected $settings_id = 'theme_options'; protected $header_logo = null; protected $header_version_text = null; protected $header_logo_link = null; protected $show_new_layout = false; protected $show_docs = false; protected $show_pages = false; protected $options_capability = 'edit_theme_options'; protected $contextual_help = array( 'content' => array(), 'sidebar' => '' );
添加选项
所有字段/选项都在doRegister()
内部执行
public function doRegister() { // Primeiro você cria a seção $this->addSection('general', 'Geral') // depois você adcionar as opções, que são automaticamente inseridas na devida seção ->addUpload('logo', 'Logo') ->addText('header_slogan', 'Header Slogan'); $this->addSection('another_section', 'Another') ->addTextArea('text_impact', 'Text impact') ->addPageSelect('my_page_id', 'Select Page'); // Você não é obrigado a encadear os metodos $this->addSection('more_section', 'GoT'); $this->addCustomPostTypeSelect('my_got_id','Select GoT', 'Desc of select', 'got'); $this->addCategorySelect('my_cat_id','Select GoT', 'Desc of select', 'got'); // As as opções são anexadas automaticamente a última seção configurada. }
可用选项
- addText
$this->addText($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
text - addTextarea
addTextarea($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
textarea - addSelect
$this->addSelect($id, $label, array $choices, $desc = null, $std = null, $section = null, array $extra = array())
选择类型字段。 - addCheckbox
$this->addCheckbox($id, $label, array $choices, $desc = null, $std = null, $section = null, array $extra = array())
复选框类型字段。 - addRadio
$this->addRadio($id, $label, array $choices, $desc = null, $std = null, $section = null, array $extra = array())
单选按钮类型字段。 - addWYSIWYG
$this->addWYSIWYG($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
所见即所得。 - addUpload
$this->addUpload($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
上传(图片) - addCustomPostTypeSelect
$this->addCustomPostTypeSelect($id, $label, $desc = null, $postType = 'post', $std = null, $section = null, array $extra = array())
具有自定义文章类型的下拉选择字段。 - addCustomPostTypeCheckbox
$this->addCustomPostTypeCheckbox($id, $label, $desc = null, $postType = 'post', $std = null, $section = null, array $extra = array())
具有自定义文章类型的复选框类型字段。 - addPageSelect
$this->addPageSelect($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有文章类型页面的下拉选择字段。 - addPageCheckbox
$this->addPageCheckbox($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有文章类型页面的复选框类型字段。 - addPostCheckbox
$this->addPageCheckbox($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有文章类型文章的复选框类型字段。 - addPostSelect
$this->addPostSelect($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有文章类型文章的下拉选择字段。 - addTaxonomySelect
$this->addTaxonomySelect($id, $label, $desc = null, $taxonomy = 'category', $std = null, $section = null, array $extra = array())
具有分类的复选框类型字段。 - addTaxonomyCheckbox
$this->addTaxonomyCheckbox($id, $label, $desc = null, $taxonomy = 'category', $std = null, $section = null, array $extra = array())
具有分类的复选框类型字段。 - addCategorySelect
$this->addCategorySelect($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有分类的下拉选择字段。 - addCategoryCheckbox
$this->addCategoryCheckbox($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有分类的复选框类型字段。 - addTagSelect
$this->addTagSelect($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有标签的下拉选择字段。 - addTagCheckbox
$this->addTagCheckbox($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
具有标签的复选框类型字段。 - addTypography
$this->addTypography($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
- addOnOff
$this->addOnOff($id, $label, $desc = null, $std = null, $section = null, array $extra = array())
- addOption
$this->addOption(array $args)
选项的原始数据。
恢复选项
恢复主题选项中保存的数据非常简单。
$option_name = OPT::get('option_name', 'default_value'); OPT::_get('option_name', 'default_value'); // echo OPT::get('option_name', 'default_value'); $option = OPT::get_nl2br('option_name', 'default_value'); // $option = nl2br(OPT::get('option_name', 'default_value')); OPT::_get_nl2br('option_name', 'default_value') // echo nl2br(OPT::get('option_name', 'default_value'));
文章类型/模型
在需要创建自定义文章类型以满足项目需求的情况下并不少见。
Potter提供了一个API,旨在简化这项工作,这项工作有时可能既乏味又复杂。
Potter的API是基于Super-CTP构建的。
创建文章类型
在主题根目录/wp-content/themes/meutema/
下创建一个名为/app/models
的文件夹。
在/app/models
文件夹中,您将为要创建的每个文章类型创建一个文件/类,类名应与文件名相同。
在进行转换时,请使用驼峰命名法来创建类,并在名称末尾添加类型或模型以更好地识别。
例如:SliderModel.php
或SliderType.php
Potter会自动创建'滑动'帖子类型。
SliderType.php
文件内容应该是这样的
<?php use Potter\Post\Type; class SliderType extends Type { protected $supports = array('title', 'thumbnail'); protected $public = false; protected $show_ui = true; public $icon = 'picture-o'; }
可用的配置
还有许多可用的配置。
<?php use Potter\Post\Type; class SliderType extends Type { public $type = 'carrocel'; // força o post type da classe protected $taxonomies = array(); protected $labels = array(); protected $capabilities = array(); protected $args = array(); protected $supports = array(); public $icon = 'dashicons-admin-post'; protected $capability_type = 'page'; protected $public; protected $show_ui; protected $description; protected $route; protected $queryArgs = array(); protected $meta_boxes = array(); }
您可以扩展Super_Custom_Post_Type.php
类中的任何属性或方法
元数据框
Super-CTP本身具有一个元数据框系统,但它有些局限。Potter使用meta-box插件,它提供了更多灵活性和选项。
要将元数据框注册到帖子类型,请向$meta_boxes
属性添加所需的选项
protected $meta_boxes = array( 'metabox-id' => array( 'title' => 'Dados do slider', 'fields' => array( array( 'name' => 'link', 'id' => 'link', 'type' => 'text', 'clone' => false, ), array( 'name' => 'Target', 'id' => 'target', 'type' => 'select', 'options' => array( '_new', '_self', ) ) ) ) );
meta-box的所有配置都可用
帖子类型查询
使用WP_Query通常很复杂,并且不提供非常美观的工作API。使用Potter执行查询变得非常容易。
实例化查询
在您希望执行查询的位置,可以使用以下命令。
// Retorna um objeto WP_Query $sliders = \Potter\Potter::model('slider')->all(); // passe como parametro o nome do model/post type que você criou // Nada muda no seu código if($query->have_posts()): while($query->have_posts()): $query->the_post() /// endwhile; endif;
有多个可用方法。
// Retorna um objeto Potter/Post/ModelQuery $slidersQuery = \Potter\Potter::model('slider'); // passe como parametro o nome do model/post type que você criou // Retorna um objeto WP_Query $slidersQuery->perPage(15)->order('author')->exclude(99)->exe(); // Retorna um objeto WP_Query $slidersQuery->perPage(15)->exe(); $slidersQuery->exe($args = array()); // Passe parametros extras diretamente para o WP_Query $slidersQuery->get(5, $args = array()) // Uma quantidade limitada de resultados // Retorna um objeto WP_Query $slidersQuery->perPage(5)->byParent(9) // Páginas filhas do ID 9 $slidersQuery->perPage(5)->exclude(get_the_ID())->byParent(9) // Páginas filhas do ID 9, menos a página atual