setting up the api for artisans cards
This commit is contained in:
parent
9708903e42
commit
80c632aebe
99
includes/api.php
Normal file
99
includes/api.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
|
||||
add_action('rest_api_init', function () {
|
||||
|
||||
/* ----------------
|
||||
BUILDING ROUTES
|
||||
-----------------*/
|
||||
|
||||
// ################ BUILD ARTISANS SEARCH RESULTS ################
|
||||
|
||||
// * BUILD MORE NEWS CARDS
|
||||
register_rest_route('metiers-patrimoine-datas/v1/build', '/artisans', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'build_search_artisan_posts_cards',
|
||||
'permission_callback' => '__return_true',
|
||||
));
|
||||
});
|
||||
|
||||
|
||||
// ################ ARTISANS ################
|
||||
|
||||
function build_search_artisan_posts_cards($request)
|
||||
{
|
||||
// write_log($request);
|
||||
$currentLanguage = esc_html($request->get_param('current-page-language')) ?? 'fr';
|
||||
$previousActivePage = esc_html($request->get_param('active-page')) ?? 1;
|
||||
$postsPerPage = esc_html($request->get_param('posts-per-page')) ?? 12;
|
||||
$taxonomy = esc_html($request->get_param('taxonomy')) ?? 'metiers';
|
||||
$StringifiedTaxonomyIds = esc_html($request->get_param('taxonomy-ids')) ?? null;
|
||||
|
||||
$taxonomyIds = explode(',', $StringifiedTaxonomyIds);
|
||||
$taxonomyIds = array_map('intval', $taxonomyIds);
|
||||
|
||||
|
||||
$activePage = is_numeric($previousActivePage) ? $previousActivePage + 1 : 1;
|
||||
|
||||
$taxQuery = array(
|
||||
array(
|
||||
'taxonomy' => $taxonomy,
|
||||
'terms' => $taxonomyIds,
|
||||
'field' => 'term_id',
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
do_action('wpml_switch_language', $currentLanguage);
|
||||
|
||||
$args = array(
|
||||
"status" => "publish",
|
||||
"post_type" => "artisans",
|
||||
"posts_per_page" => -1,
|
||||
"paged" => $activePage,
|
||||
"tax_query" => $taxQuery,
|
||||
|
||||
);
|
||||
$newsPostsDatas = new WP_Query($args);
|
||||
|
||||
|
||||
|
||||
|
||||
ob_start();
|
||||
foreach ($newsPostsDatas->posts as $key => $post) {
|
||||
$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-artisans__thumbnail')) ?? null;
|
||||
$news_type = get_the_terms($post->ID, "news_type") ?? null;
|
||||
$post_date = get_the_date('j.m.Y', $post->ID) ?? null;
|
||||
|
||||
get_template_part(
|
||||
'template-components/artisans/card-artisans',
|
||||
null,
|
||||
array(
|
||||
'card_variant' => 'activite',
|
||||
'post_ID' => $post->ID,
|
||||
'post_title' => get_the_title($post->ID),
|
||||
'post_thumbnail' => $post_thumbnail,
|
||||
'news_type' => $news_type,
|
||||
'post_date' => $post_date,
|
||||
'current_taxonomy' => $taxonomy,
|
||||
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$html_template = ob_get_clean();
|
||||
|
||||
$response_data = array(
|
||||
'html_template' => $html_template,
|
||||
'total_posts_found' => $newsPostsDatas->found_posts,
|
||||
'active_page' => $activePage,
|
||||
'max_num_pages' => $newsPostsDatas->max_num_pages,
|
||||
);
|
||||
|
||||
|
||||
$response = new WP_REST_Response($response_data);
|
||||
|
||||
$response->set_status(200);
|
||||
|
||||
return $response;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user