使用自定义类别Metabox在管理中未保存的页面类别?

时间:2010-08-26 作者:Scott B

我对此不知所措。您是否看到以下特定于noindex、nofollow复选框的代码有任何错误?元框可以很好地绘制到屏幕上,但值不会粘住。

自定义页面标题和自定义摘录的代码工作正常。

// ===================
// = 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(\'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\', \'page\', \'normal\', \'high\');
        add_meta_box(\'postexcerpt\', __(\'Excerpt\'), \'post_excerpt_meta_box\', \'page\', \'normal\', \'core\');
        add_meta_box(\'categorydiv\', __(\'Page Index Options\'), \'post_categories_meta_box_modified\', \'page\', \'side\', \'high\');
    }
}

//Adds the custom images box
function custom_post_images() {
    global $post;
    ?>
    <div class="inside">
        <textarea style="height:70px; width:100%;margin-left:-5px;" name="cb2_customHeader" id="cb2_customHeader"><?php echo get_post_meta($post->ID, \'cb2_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 &lt;img src=\'wp-content/uploads/product1.jpg\' /&gt;. To show default images, just leave this field empty</p>
    </div>
<?php
}

//Adds the custom post title box to posts
function custom_post_title() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="cb2_customTitle" id="cb2_customTitle" value="<?php echo get_post_meta($post->ID, \'cb2_customTitle\', true); ?>"></p>
        <p>Enter your custom Post Title here and it will be used for the html &lt;title&gt; for this post page and the Google link text used for this page.</p>
    </div>
<?php
}

//Adds the custom post title box to pages
function custom_post_title_page() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="cb2_customTitle" id="cb2_customTitle" value="<?php echo get_post_meta($post->ID, \'cb2_customTitle\', true); ?>"></p>
        <p>Enter your custom Page Title here and it will be used for the html &lt;title&gt; for this page and the Google link text used for this page.</p>
    </div>
<?php
}

//adds the custom categories box
function post_categories_meta_box_modified($post) {
global $post, $noindexCat, $nofollowCat;
    $noindexCat = get_cat_ID(\'noindex\');
    $nofollowCat = get_cat_ID(\'nofollow\');
    if(in_category("noindex")){ $noindexChecked = " checked=\'checked\'";} 
    if(in_category("nofollow")){ $nofollowChecked = " checked=\'checked\'";}
?>
<div id="categories-all" class="ui-tabs-panel">
    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
        <li id=\'category-<?php echo $noindexCat ?>\' class="popular-category"><label class="selectit"><input value="<?php echo $noindexCat ?>" type="checkbox" name="post_category[]" id="in-category-<?php echo $noindexCat ?>"<?php echo $noindexChecked ?> /> noindex</label></li> 
        <li id=\'category-<?php echo $nofollowCat ?>\' class="popular-category"><label class="selectit"><input value="<?php echo $nofollowCat ?>" type="checkbox" name="post_category[]" id="in-category-<?php echo $nofollowCat ?>"<?php echo $nofollowChecked ?> /> nofollow</label></li> 
        <li id=\'category-1\' class="popular-category" style="display:none;"><label class="selectit"><input value="1" type="checkbox" name="post_category[]" id="in-category-1" checked="checked"/> Uncategorized</label></li> 
    </ul>
</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
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST[\'cb2_customHeader\']) 
        {
            update_custom_meta($postID, $_POST[\'cb2_customHeader\'], \'cb2_customHeader\');
        }
        else
        {
            update_custom_meta($postID, \'\', \'cb2_customHeader\');
        }
        if ($_POST[\'cb2_customTitle\']) 
        {
            update_custom_meta($postID, $_POST[\'cb2_customTitle\'], \'cb2_customTitle\');
        }
        else
        {
            update_custom_meta($postID, \'\', \'cb2_customTitle\');
        }
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    update_post_meta($postID, $field_name, $newvalue);
}

2 个回复
最合适的回答,由SO网友:MikeSchinkel 整理而成

我不确定,但我认为简单的答案是you have not yet added "noindex" and "nofollow" as categories to your site. 转到“管理”中的“类别”页面并添加它们,然后我认为您的代码将正常工作,它为我做到了:

http://example.com/wp-admin/edit-tags.php?taxonomy=category

当然,如果您需要以编程方式添加这些类别,最好是在插件激活中使用,这样它只需在插件首次激活时运行,您可以使用以下代码:

<?php 
/** Include the WordPress Taxonomy Administration API */
require_once(ABSPATH . \'wp-admin/includes/taxonomy.php\');
$categories = get_categories(\'hide_empty=0&fields=names\');
if (!in_array(\'noindex\',$categories))
  wp_insert_category(array(\'cat_name\'=>\'noindex\'));
if (!in_array(\'nofollow\',$categories))
  wp_insert_category(array(\'cat_name\'=>\'nofollow\'));
更新:Scott B是否可能从未关联category 分类法page 职位类型?你知道的(post_type=\'page\') 默认情况下不获取类别,对吗?您需要在代码中的某个地方运行register_taxonomy_for_object_type(); 我猜你没有?

如果我是对的,那么将此添加到您的代码中几乎肯定会解决您的问题:

add_action(\'init\',\'attach_category_to_page\');
function attach_category_to_page() {
    register_taxonomy_for_object_type(\'category\',\'page\');
}

SO网友:hakre

嗯,也许这还很遥远,但你没有在保存挂钩中保存复选框值,不是吗?

顺便说一句:如果不存在,将创建更新。因此,在该函数中不需要if。最终不需要helper函数。复杂性越低,解决问题就越容易。

结束

相关推荐

Adding goodies to themes

如何在主题更新时向Arjuna-X等主题添加内容而不丢失?儿童主题是一种很好的方式,还是有其他选择?如果新版本的主题对我添加的功能具有本机支持,该怎么办?