php – Get Acf field value & Post thumbnail in JSON
Question
I am making custom wishlist for post types , and now appending all json data to div, post title and other default values like date and content I already have. But in which way I could parse my acf fields from my custom post types, and the featured image.Currently I don’t have this data in JSON , can u help me?
jQuery(document).ready(function($){
$(document).ready(function() {
var wishlistArray = JSON.parse(localStorage.getItem('wishItem'));
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php');?>',
dataType: "json", // add data type
data: { action : 'get_ajax_posts',type:'tour',list:wishlistArray.tour },
success: function( response ) {
$.each(response, function( index, value ) {
console.log(response);
$(".testi").append(
'<div class="tour_container"><div class="custom-featured"></div><img width="400" height="267" src="'????+'" class="attachment-ct-list-thumb size-ct-list-thumb wp-post-image" alt="" loading="lazy" 400w, 200w" />'+'</div><div class="tour_title"><h3>'+JSON.stringify(value.post_title)+'</h3><p>В прямом эфире пройти по мощеным улочкам Старого города и раскрыть местные секреты</p></div>');
/*'<div>' + JSON.stringify(value.post_title) + '</div><div>' + JSON.stringify(value.post_date)+ '</div><div>' +JSON.stringify(value.post_excerpt)+ '</div><div>' +JSON.stringify(value.post_category)+ '</div>');*/
});
}
});
});
});
functions.php
/* AJAX POSTS to JSON * :))) */
function get_ajax_posts() {
// Query Arguments
$args = array(
'post_type' => array($_POST["type"]),
'post_status' => array('publish'),
'posts_per_page' => -1,
'nopaging' => true,
'order' => 'ASC',
'post__in'=> $_POST["list"],
);
// The Query
$ajaxposts = get_posts( $args ); // changed to get_posts from wp_query, because `get_posts` returns an array
echo json_encode( $ajaxposts );
exit; // exit ajax call(or it will return useless information to the response)
}
// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts');
JS wishlist localstorage
var wishlistArray = JSON.parse(localStorage.getItem('wishItem'));
if(wishlistArray === null){
wishlistArray = {
tour: [],
hotel: [],
service: []
}
}
console.log(wishlistArray);
function updateLocalStorage() {
localStorage.setItem('wishItem', JSON.stringify(wishlistArray));
}
document.querySelectorAll('.wishlist-btn').forEach(item => {
item.addEventListener('click', event => {
const parsedValue = JSON.parse((item.value));
if(wishlistArray[parsedValue.type].includes(parsedValue.id)) {
} else {
if(parsedValue.type === 'tour'){
wishlistArray.tour.push(parsedValue.id);
}
else if(parsedValue.type === 'hotel'){
wishlistArray.hotel.push(parsedValue.id);
}
else if(parsedValue.type === 'service'){
wishlistArray.service.push(parsedValue.id);
}
updateLocalStorage();
}
item.classList.add("mystyle");
console.log(wishlistArray);
})
});
var printWishlist = localStorage.getItem('wishItem');
printWishlist = JSON.parse(printWishlist);
console.log("Parse data " + printWishlist);
0
1 year
2021-06-04T15:12:53-05:00
2021-06-04T15:12:53-05:00 0 Answers
0 views
0
Leave an answer