前端REST API插入自定义帖子类型并分配自定义分类

时间:2017-02-10 08:17:13

标签: wordpress api frontend assign

我正在使用此tutorial中的方法,它适用于在前端创建自定义帖子(sugestie)。

问题在于,在提交表单时,我无法找到为这些帖子分配自定义分类的方法。在后端它运作良好。

这是分类法

register_taxonomy(  
    'cat_sugestie',  //The name of the taxonomy. 
    'sugestie', //post type name
    array(  
        'labels' => array(
            'name'              => _x( 'Categorii', 'taxonomy general name', 'books' ),
            'singular_name'     => _x( 'Categorie', 'taxonomy singular name', 'books' ),
            'search_items'      => __( 'Cauta Categorii', 'books' ),
            'all_items'         => __( 'Toate Categoriile', 'books' ),
            'parent_item'       => __( 'Categorie parinte', 'books' ),
            'parent_item_colon' => __( 'Categorie parinte:', 'books' ),
            'edit_item'         => __( 'Editeaza Categorie', 'books' ),
            'update_item'       => __( 'Update Categorie', 'books' ),
            'add_new_item'      => __( 'Adauga Categorie', 'books' ),
            'new_item_name'     => __( 'Numele noii Categorii', 'books' ),
            'menu_name'         => __( 'Categorii', 'books' ),
        ),  //Display name
        'hierarchical' => true,  
        'query_var' => true,
        'show_ui' => true,
        'show_in_rest' => true,
        'rest_base' => 'cat_sugestie',
        'show_admin_column' => true,
        'rewrite' => array(
            'slug' => 'cat_sugestie', // base slug that will display before each term
            'with_front' => false // display the category base before 
        ),
        'capabilities' => array(
            'assign_terms' => 'read',
        )
    )  
); 

这就是我如何获得表格中的分类法

<div>
    <label for="post-submission-cat">
        <?php _e( 'Chose a category', 'your-text-domain' ); ?>
    </label>

    <select name="post-submission-cat" id="post-submission-cat">

    <?php if ($cats = get_terms(array('taxonomy' => 'cat_sugestie', 'hide_empty' => false) ) ) {
        foreach ($cats as $cat) { ?>
            <option value="<?php echo $cat->term_id; ?>"><?php echo $cat->name; ?></option>
        <?php   
        }
    } ?>

    </select>
</div>

这是用于提交帖子的js代码

jQuery( document ).ready( function ( $ ) {
    $( '#post-submission-form' ).on( 'submit', function(e) {
        e.preventDefault();
        var title = $( '#post-submission-title' ).val();
        var content = $( '#post-submission-content' ).val();
        var cat = parseInt($('#post-submission-cat' ).val());

        var data = {
            title: title,
            content: content,
            post_type: 'sugestie',
            status: 'pending',
            //cat_sugestie: cat  - this doesn't work
        };

        $.ajax({
            method: "POST",
            url: POST_SUBMITTER.root + 'wp/v2/sugestie',
            data: data,
            beforeSend: function( xhr ) {
                xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
            },
            success : function( response ) {
                alert( POST_SUBMITTER.success );
            },
            fail : function( response ) {
                alert( POST_SUBMITTER.failure );
            }
        });
    });
});

那么有可能实现这一目标吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

当您使用ajax代码在后端添加帖子时,您需要在数据库中添加帖子后在文件中添加此代码。

wp_set_post_terms($pppid, $post_channel, 'category');

$pppid post_id$post_channelcategory idcategorycustom taxonomy name public static string InfoParse(string input) { //string extract = input; string extract = input.Substring(0, input.IndexOf("more information")); extract = extract.Split(new string[] {" ", "\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).Last(); return extract; }

它适合你。