无法在前端POST表单中使用自定义分类

时间:2012-03-07 作者:Richard

我有一个为自定义帖子类型配置的前端帖子表单。通过使用表单,我可以轻松地发送标题、内容和图像附件,但我的自定义分类法选择没有注册。我已经用我找到的各种代码修补了这个功能,但我不确定我到底搞砸了什么。

添加新列表。php

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST[\'title\'])) {
        $title =  $_POST[\'title\'];
    } else {
        echo \'Please enter the listing name\';
    }
    if (isset ($_POST[\'description\'])) {
        $description = $_POST[\'description\'];
    } else {
        echo \'Please enter a description\';
    }

    $tags = $_POST[\'post_tags\'];

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    \'post_title\'    =>  $title,
    \'post_content\'  =>  $description,
    \'post_category\' =>  array($_POST[\'booru_locations\']),  // Usable for custom taxonomies too
    \'post_image\'    => $newupload,
    \'post_status\'   =>  \'publish\',           // Choose: publish, preview, future, draft, etc.
    \'post_type\' =>  \'booru_directory\'  //\'post\',page\' or use a custom post type if you want to
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);


    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );

//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}

} // END THE IF STATEMENT FOR FILES

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action(\'wp_insert_post\', \'wp_insert_post\');
功能。php

$labels = array(
    \'name\'                          => \'Locations\',
    \'singular_name\'                 => \'Location\',
    \'search_items\'                  => \'Search Locations\',
    \'popular_items\'                 => \'Popular Locations\',
    \'all_items\'                     => \'All Locations\',
    \'parent_item\'                   => \'Parent Location\',
    \'edit_item\'                     => \'Edit Location\',
    \'update_item\'                   => \'Update Location\',
    \'add_new_item\'                  => \'Add New Location\',
    \'new_item_name\'                 => \'New Location\',
    \'separate_items_with_commas\'    => \'Separate Locations with commas\',
    \'add_or_remove_items\'           => \'Add or remove Location\',
    \'choose_from_most_used\'         => \'Choose from most used Locations\'
    );

$args = array(
    \'label\'                         => \'Locations\',
    \'labels\'                        => $labels,
    \'public\'                        => true,
    \'hierarchical\'                  => true,
    \'show_ui\'                       => true,
    \'show_in_nav_menus\'             => true,
    \'args\'                          => array( \'orderby\' => \'term_order\' ),
    \'rewrite\'                       => array( \'slug\' => \'place\', \'with_front\' => false ),
    \'query_var\'                     => true
);

register_taxonomy( \'booru_locations\', \'booru_directory\', $args );


// Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\' => _x( \'Services\', \'Service Type\' ),
        \'singular_name\' => _x( \'Service\', \'Service\' ),
        \'search_items\' =>  __( \'Search Services\' ),
        \'popular_items\' => __( \'Popular Services\' ),
        \'all_items\' => __( \'All Services\' ),
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Services\' ),
        \'update_item\' => __( \'Update Service\' ),
        \'add_new_item\' => __( \'Add New Service\' ),
        \'new_item_name\' => __( \'New Service\' ),
        \'separate_items_with_commas\' => __( \'Separate Services With Commas\' ),
        \'add_or_remove_items\' => __( \'Add or Remove Services\' ),
        \'choose_from_most_used\' => __( \'Choose From Most Used Services\' )
    );

register_taxonomy( \'services\', \'booru_directory\', array(
        \'hierarchical\' => false,
        \'labels\' => $labels, /* NOTICE: the $labels variable here */
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'services\' ),
    ));




register_post_type( \'booru_directory\',
    array(
        \'labels\'                => array(
            \'name\'              => __( \'Directory\' ),
            \'singular_name\'     => __( \'Listing\' )
            ),
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\' ),
        \'rewrite\'               => array( \'slug\' => \'directory\', \'with_front\' => false ),
        \'has_archive\'           => true
    )
);

1 个回复
SO网友:Stephen Harris

看看后面的抄本wp_insert_post. 我没有检查,但我认为这不正确:

 \'post_category\' =>  array($_POST[\'booru_locations\']),  // Usable for custom taxonomies too
传递给的数组wp_insert_post, 但是可以包含tax_input 键,其值是数组的数组:

 \'tax_input\' =>array( \'booru_locations\' => array( \'term\', \'term2\', \'term3\' ) )
“booru\\u locations”的值必须是字符串数组(对于术语段塞)或整数数组(对于术语ID)。如果您使用的是术语ID,则必须通过将它们转换为整数来确保它们是整数。


So$_POST[\'booru_locations\'] 包含分类法“booru\\u位置”的术语ID。首先,确保它是一个整数(不是的字符串表示形式),并创建一个ID数组(在这种情况下,只有一个ID):

 $location_id = intval($_POST[\'booru_locations\']);
 $locations = (!empty($location_id) ? array($location_id) : array());
所以$locations 整数数组(术语ID)。然后我们可以将此添加到$new_post 大堆

// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
\'post_title\'    =>  $title,
\'post_content\'  =>  $description,
\'post_image\'    => $newupload,
\'post_status\'   =>  \'publish\',  
\'post_type\' =>  \'booru_directory\',
\'tax_input\' =>  array(\'booru_locations\'=>$locations),
);

结束

相关推荐

prefix table and plugins

出于安全原因和良好实践等原因,我将我表的前缀从wp\\u1改为其他形式。我很好奇,大多数安装的新插件都采用了新前缀,但我有一个插件拒绝使用wp\\u1,并且仍然使用wp\\u2安装。它仍然有效,但我很好奇,这是正常的做法还是一个写得不好的插件?