添加页面属性metabox和页面模板到帖子编辑页面?
-
16-10-2019 - |
题
(主持人注意: 标题最初是“我如何添加“页面属性”和/或“页面属性”>“ selector to ports Editor”)
WP当前仅允许将“模板”分配到页面(即 post_type=='page'
。)我也想将此功能扩展到帖子(即 post_type=='post'
.)
我该如何添加 “页面属性” 元框,更具体地说,是帖子编辑器的模板切换器?
我假设这是我将放在我的 functions.php
为了我的主题。
更新:通过将Select Box HTML添加到我现有的自定义Meta选项框中,我设法将硬编码模板Pulldown菜单添加到我的帖子编辑器中。这是我正在使用的代码...
add_meta_box('categorydiv2', __('Post Options'), 'post_categories_meta_box_modified', 'post', 'side', 'high');
这是写入选项和模板选择框的功能...
//adds the custom categories box
function post_categories_meta_box_modified() {
global $post;
if( get_post_meta($post->ID, '_noindex', true) ) $noindexChecked = " checked='checked'";
if( get_post_meta($post->ID, '_nofollow', true) ) $nofollowChecked = " checked='checked'";
?>
<div id="categories-all" class="ui-tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
<li id='noIndex' class="popular-category"><label class="selectit"><input value="noIndex" type="checkbox" name="chk_noIndex" id="chk_noIndex"<?php echo $noindexChecked ?> /> noindex</label></li>
<li id='noFollow' class="popular-category"><label class="selectit"><input value="noFollow" type="checkbox" name="chk_noFollow" id="chk_noFollow"<?php echo $nofollowChecked ?> /> nofollow</label></li>
</ul>
<p><strong>Template</strong></p>
<label class="screen-reader-text" for="page_template">Post Template</label><select name="page_template" id="page_template">
<option value='default'>Default Template</option>
<option value='template-wide.php' >No Sidebar</option>
<option value='template-salespage.php' >Salespage</option>
</select>
</div>
<?php
}
最后,在保存...上捕获所选值的代码...
function save_post_categories_meta($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
$noIndex = $_POST['chk_noIndex'];
$noFollow = $_POST['chk_noFollow'];
update_post_meta( $post_id, '_noindex', $noIndex );
update_post_meta( $post_id, '_nofollow', $noFollow );
return $post_id;
}
现在,我相信剩下的就是(1)捕获所选模板并将其添加到本帖子的元元中,以及(2)修改index.php和single.php,以便它使用所选的模板。
解决方案
讨厌成为坏消息的承载者 WordPress Hardcodes页面模板功能到 “页” 帖子类型, ,至少在v3.0中 (这可能会改变未来的版本,但我没有知道要改变它的特定计划。因此,这是我很少努力地努力弄清楚如何在没有黑客核心的情况下解决某事的几次。)
我想出的解决方案是从WordPress Core基本上复制相关代码并根据我们的需求进行修改。这是步骤(行号来自v3.0.1):
复制
page_attributes_meta_box()
功能 从第535行/wp-admin/includes/meta-boxes.php
并修改适合。代码an
add_meta_boxes
钩 添加在#1中创建的Metabox。复制
get_page_templates()
功能 从第166行/wp-admin/includes/theme.php
并修改适合。复制
page_template_dropdown()
功能 从第2550行/wp-admin/includes/template.php
并修改适合。添加帖子模板 主题。
代码a
save_post
钩 在保存时启用存储邮政模板文件名。代码a
single_template
钩 为了启用关联帖子的邮政模板的加载。
现在继续!
1.复制 page_attributes_meta_box()
功能
作为我们的第一步,您需要复制 page_attributes_meta_box()
从第535行的功能 /wp-admin/includes/meta-boxes.php
我选择重命名 post_template_meta_box()
. 。由于您仅要求提供页面模板,因此我省略了用于指定父帖的代码,并指定了使代码更简单的订单。我还选择使用Postmeta为此而不是尝试重复使用 page_template
对象属性,以避免和无意耦合引起的潜在不兼容。因此,这是代码:
function post_template_meta_box($post) {
if ( 'post' == $post->post_type && 0 != count( get_post_templates() ) ) {
$template = get_post_meta($post->ID,'_post_template',true);
?>
<label class="screen-reader-text" for="post_template"><?php _e('Post Template') ?></label><select name="post_template" id="post_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php post_template_dropdown($template); ?>
</select>
<?php
} ?>
<?php
}
2.代码an add_meta_boxes
钩
下一步是使用 add_meta_boxes
钩:
add_action('add_meta_boxes','add_post_template_metabox');
function add_post_template_metabox() {
add_meta_box('postparentdiv', __('Post Template'), 'post_template_meta_box', 'post', 'side', 'core');
}
3.复制 get_page_templates()
功能
我认为区分页面模板和邮政模板只有很有意义 get_post_templates()
基于功能 get_page_templates()
从第166行 /wp-admin/includes/theme.php
. 。但是而不是使用 Template Name:
标记哪个页面模板使用此功能使用 Post Template:
标记,您可以在下面看到。
我还过滤了检查 functions.php
(不确定如何 get_page_templates()
曾经没有这样做正确的工作,但是无论如何!) 唯一剩下的就是更改对单词的引用 page
至 post
为了维护可读性:
function get_post_templates() {
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates = array();
if ( is_array( $templates ) ) {
$base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
foreach ( $templates as $template ) {
$basename = str_replace($base, '', $template);
if ($basename != 'functions.php') {
// don't allow template files in subdirectories
if ( false !== strpos($basename, '/') )
continue;
$template_data = implode( '', file( $template ));
$name = '';
if ( preg_match( '|Post Template:(.*)$|mi', $template_data, $name ) )
$name = _cleanup_header_comment($name[1]);
if ( !empty( $name ) ) {
$post_templates[trim( $name )] = $basename;
}
}
}
}
return $post_templates;
}
4.复制 page_template_dropdown()
功能
同样复制 page_template_dropdown()
从第2550行 /wp-admin/includes/template.php
去创造 post_template_dropdown()
然后将其更改为致电 get_post_templates()
反而:
function post_template_dropdown( $default = '' ) {
$templates = get_post_templates();
ksort( $templates );
foreach (array_keys( $templates ) as $template )
: if ( $default == $templates[$template] )
$selected = " selected='selected'";
else
$selected = '';
echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
endforeach;
}
5.添加邮政模板
下一步是添加一个用于测试的邮政模板。使用 Post Template:
步骤3副本中提到的标记 single.php
从你的主题到 single-test.php
并添加以下评论标题(确保修改某些内容 single-test.php
因此,您可以知道它正在加载而不是 single.php
):
/**
* Post Template: My Test Template
*/
一旦完成步骤#1到达#5,您就可以看到您的 “邮政模板” Metabox出现在您的邮政编辑器页面上:
(资源: mikeschinkel.com)
6.代码a save_post
钩
现在,您已经有了编辑器,当用户单击“发布”时,您需要将页面模板名称实际上保存到postmeta。这是该代码:
add_action('save_post','save_post_template',10,2);
function save_post_template($post_id,$post) {
if ($post->post_type=='post' && !empty($_POST['post_template']))
update_post_meta($post->ID,'_post_template',$_POST['post_template']);
}
7.代码a single_template
钩
最后,您实际需要让WordPress使用新的帖子模板。你通过钩子做到这一点 single_template
并为那些已分配的帖子返回所需的模板名称:
add_filter('single_template','get_post_template_for_template_loader');
function get_post_template_for_template_loader($template) {
global $wp_query;
$post = $wp_query->get_queried_object();
if ($post) {
$post_template = get_post_meta($post->ID,'_post_template',true);
if (!empty($post_template) && $post_template!='default')
$template = get_stylesheet_directory() . "/{$post_template}";
}
return $template;
}
仅此而已!
笔记 我 没有 考虑一下 自定义帖子类型, , 只要 post_type=='post'
. 。在我看来,解决自定义帖子类型的意见将需要区分不同的帖子类型,虽然不太困难,但我在这里并没有尝试过。
其他提示
WordPress允许您使用插件添加元数据:
为此,您需要添加各种扩展程序之一,这些扩展名将元数据添加到类别中(模仿哪些页面开箱即用), 简单术语元 工作很好。
NB WordPress 3.x是扩展类别所需的。
之后,您可以使用:
- add_term_meta
- update_term_meta
- get_term_meta
使用functions.php添加方法来完成您想要的事情
add_action('category_add_form_fields', 'category_metabox_add', 10, 1);
function category_metabox_add($tag) { ?>
<div class="form-field">
<label for="image-url"><?php _e('Image URL') ?></label>
<input name="image-url" id="image-url" type="text" value="" size="40" aria-required="true" />
<p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
</div>
<?php }
add_action('created_category', 'save_category_metadata', 10, 1);
function save_category_metadata($term_id)
{
if (isset($_POST['image-url']))
update_term_meta( $term_id, 'image-url', $_POST['image-url']);
}
在主题中调用新字段很容易:
<?php echo get_term_meta(get_query_var('cat'), 'image-url', true); ?>
更多详细信息和示例:http://www.wphub.com/adding-metadata-taxonomy-terms/