我有一个自定义字段,我想在帖子标题输入后放置该字段。
如何在编辑器之前更改顺序或包含自定义字段?
这是我的代码:
function info_posts() {
add_meta_box(
'intro_post', // $id
'Introducción al post', // $title
'meta_info', // $callback
'post', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'info_posts');
function meta_info() {
global $post;
$meta = get_post_meta($post->ID, 'intro_post', true);
echo '<input type="hidden" name="info_post" value="'.wp_create_nonce(basename(__FILE__)).'" />';
echo '<table class="form-table">';
echo '<tr>
<td>
<strong>Recomendado:</strong><span class="description"> Ingresar hasta 52 palabras / 400 caracteres.</span><br/>
Total ingresado: '.str_word_count(get_post_meta($post->ID, 'intro_post', true) ).' palabras <span class="description"> (se actualiza al guardar)</span>.<br/><br/>
<textarea name="intro_post" id="intro_post" cols="95" rows="8">'.$meta.'</textarea><br/><br/>
<strong>Pequeños tips:</strong> <br/>
- Cada 25 palabras, añade uno o dos saltos de línea, ingresando el siguiente código: <input onclick="select()" value="<br/><br/>" size="7" style="border-radius:3px;border:1px solid #dcdcdc;background:#eee;color:#444"><br/>
- El texto será ubicado a la izquierda de la imagen destacada del post, procura detallar lo mejor posible el mismo.
</td>
</tr>';
echo '</table>';
}
function guardar_intro($post_id) {
if (!wp_verify_nonce($_POST['info_post'], basename(__FILE__)))
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
$old = get_post_meta($post_id, "intro_post", true);
$new = $_POST["intro_post"];
if ($new && $new != $old) {
update_post_meta($post_id, "intro_post", $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, "intro_post", $old);
}
}
add_action('save_post', 'guardar_intro');
我喜欢做什么。
可以帮助我吗?
谢谢。
最佳答案
我刚刚找到了解决方案。
代码:
// Move all "advanced" metaboxes above the default editor
add_action('edit_form_after_title', function() {
global $post, $wp_meta_boxes;
do_meta_boxes(get_current_screen(), 'advanced', $post);
unset($wp_meta_boxes[get_post_type($post)]['advanced']);
});
无论如何,谢谢你。
关于php - WordPress - 输入帖子标题后的自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545825/