S
S
Sergey Burduzha2021-11-30 12:10:15
API
Sergey Burduzha, 2021-11-30 12:10:15

How to get taxonomies in another language using wpblobus?

Good afternoon.
On the page I display taxonomies in the sidebar and posts.
I filter using vue.

Created 2 api for taxonomies

function cats_register_search()
{
    register_rest_route('cats/v1', 'search', [
        'methods' => WP_REST_SERVER::READABLE,
        'callback' => 'catsResults',
    ]);
}

add_action('rest_api_init', 'cats_register_search');
function catsResults()
{
    $terms = get_terms([
        'taxonomy' => 'cat',
        'hide_empty' => true,
    ]);

    return $terms;
}


And posts

function portfolio_register_search()
{
    register_rest_route('portfolio/v1', 'search', [
        'methods' => WP_REST_SERVER::READABLE,
        'callback' => 'portfolioSearchResults',
    ]);
}

add_action('rest_api_init', 'portfolio_register_search');
function portfolioSearchResults($data)
{
    $portfolio_result = [];

    if ($data['term'] === "all") {
        $portfolio = new WP_Query([
            'post_type' => 'portfolio',
            'posts_per_page' => -1
        ]);
    } else {
        $portfolio = new WP_Query([
            'post_type' => 'portfolio',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'cat',
                    'field' => 'slug',
                    'terms' => $data['term']
                )
            )
        ]);
    }

    while ($portfolio->have_posts()) {
        $portfolio->the_post();
        $id = get_the_ID();
        $term = get_the_terms($id, 'cat')[0];
        $term_name = $term->name;
        $url = get_field('single_portfolio')['link'];
        $parent_term = get_term($term->parent, 'cat');

        array_push($portfolio_result, [
            'title' => get_the_title(),
            'img' => get_the_post_thumbnail_url(get_the_ID(), 'full'),
            'term_name' => $term_name,
            'url' => $url,
            'parent_term' => $parent_term
        ]);
    }
    return $portfolio_result;
}


And on the page I filter posts by taxonomies.

const appFilter = new Vue({
        el: "#search",
        data: {
            home_url: '',
            filtered: [],
            cat_search: 'all',
            cats: [],
            cats_parent: [],
            cats_child: [],
            cats_filtered: [],
            loading: true
        },
        methods: {
            filterProducts(term) {
                this.loading = true;
                this.cat_search = term;
                fetch(this.home_url + '/wp-json/portfolio/v1/search?term=' + this.cat_search)
                    .then(response => response.json())
                    .then(res => {
                        this.filtered = res;
                        console.log(this.filtered, 'this.filtered')
                        setTimeout(() => {
                            this.loading = false;
                        }, 200);
                    })
                    .catch(error => console.log(error, 'error'))
            }
        },
        mounted() {
            this.home_url = window.location.origin;
            fetch(this.home_url + '/wp-json/cats/v1/search')
                .then(response => response.json())
                .then(res => {
                    this.cats = res;
                    if (Object.prototype.toString.call(this.cats) !== '[object Array]') {
                        this.cats = Object.values(this.cats);
                    }
                    this.cats_parent = this.cats.filter(item => item.parent === 0);
                    this.cats_child = this.cats.filter(item => item.parent !== 0);
                    this.cats_filtered = this.cats_parent.map(item => {
                        return {
                            parent: {
                                "name": item.name,
                                "slug": item.slug
                            },
                            children: this.cats_child.filter(child => child.parent === item.term_id)
                        };
                    });
                    this.filterProducts('all')
                })
                .catch(error => console.log(error, 'error'))
        }
    });

The task has changed, now you need to add several languages.
The problem is that get_terms on the worpdress page are automatically translated when changing the language, but how do I do it in the file where I create the api.
I understand that I can set the parameter / en, / ru, but how to get taxonomies and posts with translation?

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question