有没有一种方法可以使用rest_api和apppresser在1个php插件中显示2个函数?

时间:2019-10-02 14:36:01

标签: php rest wordpress-rest-api

我创建了一个插件来在Apppresser中的博客文章上显示评论,但是一次只能使用其中一个功能。第一个功能显示帖子中的评论,第二个功能应该是通过CSS(#respond)链接到评论部分的简单链接

我尝试确保所有功能都是唯一的。我尝试将这些功能合并为1,但是未能正确显示信息。

add_action( 'rest_api_init', 'appp_register_my_template_hook' );
function appp_register_my_template_hook() {
    register_rest_field( 'post', // any post type registered with API
        'appp',
        array(
            'get_callback'    => 'appp_comments',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
function appp_comments( $object, $field_name, $request ) {
    $data = [];
    $comments = get_comments( array( 'post_id' => $object['id'] ) );
    foreach($comments as $comment) :
        $data['post_detail']['below_content'] .= '<div class="comment"><h4>' . $comment->comment_author . '</h4><p>' . $comment->comment_content . '</p></div>';
    endforeach;
    return $data;
}

add_action( 'rest_api_init', 'appp_register_template_hook' );
function appp_register_template_hook() {
    register_rest_field( 'post', // any post type registered with API
        'appp',
        array(
            'get_callback'    => 'appp_get_hook_data',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
/**
 * Get the value of a meta field field
 *
 * @param array $object Details of current post.
 * @param string $field_name Name of field.
 * @param WP_REST_Request $request Current request
 *
 * @return mixed
 */
function appp_get_hook_data( $object, $field_name, $request ) {
    $data = [];
    // Add a featured image to the single post view in the app
    $data['post_detail']['below_title'] = '<div><a href="' . get_the_permalink( $object['id'] ) . '#respond">Drop A Comment</a></div>';
    return $data;
}

它当前显示拖放评论链接,但不显示评论循环。

0 个答案:

没有答案