作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
下面的代码几乎可以完美运行,但是我的其中一个页面上的页面标题值在刷新几页后一直为空...它停留了一段时间,然后似乎重置为空。我想我一定在下面的代码中有冲突,但我不太明白。
我允许用户通过自定义“帖子/页面标题输入字段”为帖子和页面设置自定义页面标题。任何人都可以在这里看到一个明显的问题,可能会将页面标题重置为空白吗?
// ===================
// = POST OPTION BOX =
// ===================
add_action('admin_menu', 'my_post_options_box');
function my_post_options_box() {
if ( function_exists('add_meta_box') ) {
//add_meta_box( $id, $title, $callback, $page, $context, $priority );
add_meta_box('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low');
add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high');
add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high');
add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core');
}
}
//Adds the custom images box
function custom_post_images() {
global $post;
?>
<div class="inside">
<textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"><?php echo get_post_meta($post->ID, 'customHeader', true); ?></textarea>
<p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so <img src='wp-content/uploads/product1.jpg' />. To show default images, just leave this field empty</p>
</div>
<?php
}
//Adds the custom post title box
function custom_post_title() {
global $post;
?>
<div class="inside">
<p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="<?php echo get_post_meta($post->ID, 'customTitle', true); ?>"></p>
<p>Enter your custom post/page title here and it will be used for the html <title> for this post page and the Google link text used for this page.</p>
</div>
<?php
}
add_action('save_post', 'custom_add_save');
function custom_add_save($postID){
if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
return $postID;
}
else
{
// called after a post or page is saved and not on autosave
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['customHeader'])
{
update_custom_meta($postID, $_POST['customHeader'], 'customHeader');
}
else
{
update_custom_meta($postID, '', 'customHeader');
}
if ($_POST['customTitle'])
{
update_custom_meta($postID, $_POST['customTitle'], 'customTitle');
}
else
{
update_custom_meta($postID, '', 'customTitle');
}
}
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
?>
最佳答案
Wordpress 的自动保存系统很可能是您的问题,因为我认为自定义字段不会传递给自动保存(因此您的 customHeader
和 customTitle
post 变量将为空在自动保存期间)。
在您的保存函数中,您应该检查是否设置了 DOING_AUTOSAVE
常量(这似乎比检查后操作更可取),如果是则返回。像这样:
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
有关更多信息,请参阅此票证:http://core.trac.wordpress.org/ticket/10744
关于php - WordPress add_meta_box() 怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2539951/
下面的代码几乎可以完美运行,但是我的其中一个页面上的页面标题值在刷新几页后一直为空...它停留了一段时间,然后似乎重置为空。我想我一定在下面的代码中有冲突,但我不太明白。 我允许用户通过自定义“帖子/
我是一名优秀的程序员,十分优秀!