在快速编辑模式下检索自定义字段的值
-
16-10-2019 - |
题
我一直在尝试添加一个字段来快速编辑。它以某种方式起作用:它显示为显示,如果您在输入字段中输入值,则将值保存到自定义字段中。但是,似乎找不到一种检索自定义字段价值的方法。这是我到目前为止得到的:
add_action('quick_edit_custom_box', 'ilc_quickedit_show', 10, 2);
function ilc_quickedit_show( $col, $type ) {
if( $type != 'event' ) return; ?>
<fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<div class="inline-edit-group">
<label for="eventdate" style="font: italic 12px Georgia, serif;">Event Date</label>
<span class="input-text-wrap">
<input type="text" name="eventdate" id="eventdate" size="10" value="">
</span>
</div>
</div>
</fieldset>
<?php
}
使用以下方式保存输入的值:
add_action('save_post','ilc_quickedit_save',10,3);
function ilc_quickedit_save($post_id, $post) {
if( $post->post_type != 'event' ) return;
update_post_meta($post_id, 'Event Date', $_POST['eventdate']);
}
正如您肯定注意到的那样,这是针对自定义帖子类型的“事件”。但是,我无法检索值并填充字段。 afaik,这涉及Inline-edit-post.js,但我找不到任何使用InlineDitPost检索自定义字段值的方法。甚至帖子ID都在JavaScript范围中可用
add_action('admin_head-edit.php', 'ilc_quickedit_get');
function ilc_quickedit_get() { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('a.editinline').live('click', function() {
var id = inlineEditPost.getId(this);
alert("Post id: " + id);
});
});
</script>
<?php
}
下载了自定义字段模板插件以剖析代码,并发现它们正在重新定义InlineDitPost函数的部分,因此我考虑过要做同样的事情。但是,他们似乎是通过存储重复的选项阵列进行的。如果您解决了这个问题,您是否可以分享您用来检索每个自定义字段的值的内容?
解决方案
好的,我明白了,这是代码:
function ilc_quickedit_save($post_id, $post) {
if( $post->post_type != 'evento' ) return;
if (isset($_POST['is_quickedit']))
update_post_meta($post_id, 'eventdate', $_POST['eventdate']);
}
function ilc_quickedit_get() {
$html = '<script type="text/javascript">';
$html .= 'jQuery(document).ready(function() {';
$html .= 'jQuery("a.editinline").live("click", function() {';
$html .= 'var id = inlineEditPost.getId(this);';
$html .= 'jQuery.post("' . THEME_URI . '/library/admin/admin.php",{ post_id: id, modo: "ajaxget" },';
$html .= 'function(data){ jQuery("#eventdate").val(data); }';
$html .= ');});});';
$html .= '</script>';
echo $html;
}
以及Admin.php文件开头的代码(这是所有代码所在的文件):
if($_POST['modo'] == 'ajaxget'){
require_once('../../../../../wp-blog-header.php');
$post_id = $_POST['post_id'];
echo get_post_meta($post_id, 'eventdate', true);
return;
}
到目前为止,也许还有其他方法可以做到。仍然需要在批量编辑模式下处理保存。也可以在那里用手:)希望这对某人很有用。
其他提示
这是 未经测试 代码。看看它是否适合您。基本上,正如您将在下面阅读的那样,我全球化 $post
, ,检查您的CPT,然后检索自定义字段 event
从帖子的元(返回单个值 - 由最后一个参数中的“ true”设置)。
您唯一要做的就是参考 $event
在您的jQuery中这样: {$event}
将值输出到JavaScript中。它会显示为“静态”,但实际上它是动态的。
add_action('admin_head-edit.php', 'ilc_quickedit_get');
function ilc_quickedit_get()
{
global $post;
if ( $post->post_type != "event" )
return;
$event = get_post_meta( $post->ID, 'event', true ); // gets a *single* option from the postmeta table
$html = <<<HTML
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('a.editinline').live('click', function() {
// do something with {$event}
});
});
</script>
HTML;
echo $html;
}
PS 我将您的HTML代码分配给PHP变量,以使其更易于阅读并允许您快速参考 $event
在jQuery中。