@EvgenyKabakov

Как настроить REST API Wordress через плагин acf-to-rest-api?

Всем доброго времени суток!
Имеется php код, создающий в админ-панели сайта
на Wordpress кастомный тип записей (cars_items в коде ниже) .
К этому типу записей добавлены custom fields плагином ACF.
Прошу подсказать, каким образом можно вывести в REST API в формате JSON
привязанные к этому кастомному типу поля ACF из всех постов этого кастомного типа?
Пытаюсь это сделать с помощью плагина acf-to-rest-api, но никак не получается,
Единственное, что получается отобразить по ссылка-пример другого сайта, но здесь не выводится нужных данных.
Нагуглить что-либо похожее не удалось. Прошу помощи у знатоков : )

PHP-код создания кастомного типа записей:
final class cars_items {
    // Ярлык произвольного типа записи по умолчанию
    private $post_type = 'cars_items';

    function __construct( $post_type = "" ) {
        // Переопределяем значение ярлыка по умолчанию
        if ( $post_type )
            $this -> post_type = $post_type;

        /*
         * Регистрируем Custom Post Type
         */
        add_action( 'init', array( $this, 'cars_item_prod' ) );

        /*
         * Фильтруем URL
         */
        add_filter( 'post_type_link', array( $this, 'product_permalink_structure' ), 10, 2 );

        /*
         * Чтобы работала пагинация
         */
        add_action( 'generate_rewrite_rules', array( $this, 'fix_product_category_pagination' ) );
    }

    function cars_item_prod() {
        /*
         * Регистрируем произвольную таксономию к новому типу записей
         */
        register_taxonomy(
            'cars_items_type', 'cars_items', array(
            'label' => 'Типы',
            'hierarchical' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => $this -> post_type ),
          )
        );
        /*
         * Регистрируем новый тип записи
         */
        $labels = array(
            'name' => 'Автомобили', // Основное название
            'singular_name' => 'Автомобили', // Добавить
            'add_new' => 'Добавить автомобиль', // Имя ссылки на новую запись в сайдбаре
            'add_new_item' => 'Добавить автомобиль', // Заголовок в редакторе при добавлении новой записи
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_in_menu' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => 11,
			"menu_icon" => "dashicons-car",
            'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
            'rewrite' => array(
                'slug' => '/cars',
                'with_front' => false,
            ),
            'has_archive' => $this -> post_type,
        );
        register_post_type( 'cars_items', $args );
        if ( current_user_can( 'manage_options' ) ) {

        }
    }

    function product_permalink_structure( $post_link, $post ) {
        if ( FALSE !== strpos( $post_link, '%cars_items_type%' ) ) {
            $product_type_term = get_the_terms( $post -> ID, 'cars_items_type' );
            if ( !empty( $product_type_term ) )
                $post_link = str_replace( '%cars_items_type%', $product_type_term[0] -> slug, $post_link );
        }
        return $post_link;
    }

    function fix_product_category_pagination( $wp_rewrite ) {
        unset( $wp_rewrite -> rules[ $this -> post_type . '/([^/]+)/page/?([0-9]{1,})/?$'] );
        $wp_rewrite -> rules = array(
            $this -> post_type . '/?$' => $wp_rewrite -> index . '?post_type=cars_items',
            $this -> post_type . '/page/?([0-9]{1,})/?$' => $wp_rewrite -> index . '?post_type=cars_items&paged=' . $wp_rewrite -> preg_index( 1 ),
            $this -> post_type. '/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite -> index . '?cars_items_type=' . $wp_rewrite -> preg_index( 1 ) . '&paged=' . $wp_rewrite -> preg_index( 2 ),
                ) + $wp_rewrite -> rules;
    }

}
/*
 * Запускаем класс
 * В скобках можно определить название ярлыка типа записи
 */
new cars_items( 'Автомобили' );
  • Вопрос задан
  • 78 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы