1
현재 내 주제는 AJAX로 게시물을로드합니다. 하지만 AJAX를 통해 메타 키를 게시하려면 어떻게해야합니까? 현재 함수 PHP에서Wordpress metakeys를 AJAX로 로딩하기
은 다음과 같이이다 :
add_action('wp_ajax_nopriv_ajax_action', 'ajax_loading');
add_action('wp_ajax_ajax_action', 'ajax_loading');
function ajax_loading() {
switch($_REQUEST['fn']) {
case 'get_latest_posts':
$output = ajax_get_latest_posts($_REQUEST['count']);
break;
default:
$output = 'Error. No function specified.';
break;
}
$output = json_encode($output);
if (is_array($output)) {
print_r($output);
}
else {
echo $output;
}
die;
}
function ajax_get_latest_posts($count) {
$posts = get_posts('numberposts='.$count.'&post_status=publish');
return $posts;
}
그리고 jQuery를 :
$.ajax({
url: 'http://domain.com/wp-admin/admin-ajax.php',
type: 'POST',
data: {
'action': 'ajax_action',
'fn': 'get_latest_posts',
'count': 15
},
dataType: 'JSON',
success:function(data){
//print stuff here
},
error: function(errorThrown){
//error stuff here
}
})
이 코드 난 단지 주요 포스트 정보를 얻을 (제목, ID, 날짜, 내용으로 ..).
게시물에서 AJAX/JS로 메타 키/값을 가져 오려고 시도한 사람이 있습니까?
흠을 사용하여 각
posts
에 '메타'키를 추가 한, 그것은 오류가 있습니다 : 유형의 개체를 사용할 수 없습니다 배열로 WP_Post. 이 줄을위한 것입니다 : $ posts [$ key] [ 'meta'] = get_post_meta ($ post-> ID); – user995317죄송합니다, Wordpress env를 사용하고 있지 않습니다. $ posts는 WP 객체의 배열이고 배열이 아니기 때문에 아마도 $ posts [$ key] -> meta = get_post_meta ($ post-> ID);가되어야합니다. – yoavmatchulsky
실제로, 나는 내 자신을 생각해야합니다. 알았어. 고마워요! – user995317