frojd / frojd-segments
该包最新版本(v1.0.2)没有可用的许可信息。
v1.0.2
2015-11-13 10:00 UTC
Requires
- php: >=5.3.2
- composer/installers: v1.0.6
This package is not auto-updated.
Last update: 2024-10-02 09:51:52 UTC
README
一个插件,用于处理段元框,编辑器可以从可用文章列表中拖放文章到排序列表中,然后可以在主题中以不同的方式使用。此功能主要从“特色文章”的角度构建,例如,编辑器可以为特定文章选择相关文章。
使用主题中包含的钩子,可以将段元框添加到特定文章的admin编辑视图中。到目前为止,只有添加到特定帖子类型的元框有实现。
用法
以下是创建段元框和将其放入模板中的示例。其他示例可以在示例目录中查看。
将元框添加到编辑视图
"frojd_segments_metabox" 动作可以在主题中的任何位置调用,最有可能在 functions.php 中。通过向动作钩子发送特定参数,该类型的文章的admin编辑视图中将显示元框。应在由 "add_meta_boxes" 动作触发的函数内调用此动作。每个段元框将显示从 "selection" 参数中选定的所有可用文章的列表。当将项目拖放到选定列表中时,"options" 参数中定义的选项将在点击设置图标时打开的小弹出窗口中可用。
示例
add_action('add_meta_boxes', 'addMetaBoxesHook'); function addMetaBoxesHook() { $metaboxes = array(); $metaboxes['segments_metabox'] = array( 'title' => __('Select my articles', get_translation_domain()), // defaults to "Select articles" 'selection' => array( // defaults to all posts, same parameters as in get_posts can be used here 'post_type' => array('page') ), 'show_on' => array( 'post_types' => array('page'), // defaults to page 'valid_terms' => array( // defaults to none, uses same parameters as has_term, term can be name/id/slug or array of them to check for, taxonomy is the name array('taxonomy' => 'term') ), 'post_meta' => array( // defaults to none, uses get_post_meta function to check, key is the field name and value is what to check for array('key' => 'value') ) ), 'options' => array( // Options to be added to each item selected in the segment, can be used to e.g. implement grid layout or add an alternative title. 'layout' => array( 'label' => __('Grid layout', get_translation_domain()), 'type' => 'select', // Can be any text input field (text, number etc), select, checkbox or textarea 'options' => array( // Key and value of the options in select e.g. array('quarter' => '1/4') 'value' => 'name' ) ) ) ); do_action('frojd_segments_metabox', $metaboxes); }
从帖子中检索段
可以通过不同方式检索元框和选定的帖子。过滤器 "frojd_segments_get_segment" 可以接受两个参数(帖子 ID 和元框 ID),并返回选定的段。过滤器 "frojd_segments_get_segments" 可以接受一个参数(帖子 ID),并返回包含该帖子所有元框的整个段对象,包括在该段中选定的帖子。否则,实现段的最简单方法是通过挂钩到 "the_post" 函数并给帖子对象添加一个段属性,如以下示例所示。完成此操作后,段对象在全局 $post 对象可用的每个循环中均可用。
示例
add_action('the_post', 'thePostHook'); function thePostHook($post) { // Adds a new value to the post object containing the post segments $segments = apply_filters('frojd_segments_get_segments', $post->ID); $post->segments = (object) $segments; }