2012-09-03 46 views
0

我遇到了ACF和AJAX的問題:自定義字段在加載時沒有被調用到AJAX投資組合中。使用AJAX/jQuery調用高級自定義字段

我是相對較新的AJAX & PHP並不確定如何解決這個問題。

add_action("wp_ajax_eq_get_ajax_project", "eq_get_ajax_project"); 
add_action("wp_ajax_nopriv_eq_get_ajax_project", "eq_get_ajax_project"); 

function eq_get_ajax_project() { 

if (!wp_verify_nonce($_REQUEST['nonce'], "portfolio_item_nonce")) { 
    exit("No naughty business please"); 
}  

$grid_classes = 'grid_9 alpha'; 
$quality = 90; 
$desired_width = 700; 
$desired_height = 500; 
$current_post_id = $_REQUEST['post_id']; 
$video_embed_code = get_post_meta($_REQUEST['post_id'], 'portfolio-video-embed', true); 
$portfolio_images = eq_get_the_portfolio_images($_REQUEST['post_id']); 
$terms = get_the_terms($_REQUEST['post_id'] , 'portfolio_categories', 'string'); 
$content_post = get_post($_REQUEST['post_id']); 
$content = $content_post->post_content; 
$content = apply_filters('the_content', $content); 
$content = str_replace(']]>', ']]>', $content); 
$post = get_post($current_post_id); 
$client = get_post_meta($current_post_id, 'oy-client', true); 
$project_url = get_post_meta($current_post_id, 'oy-item-url', true); 

ob_start(); 
?> 

這是我的jQuery文件:

jQuery(document).ready(function($) { 

/*-----------------------------------------------------------------------------------*/ 
/* Ajax Call 
/*-----------------------------------------------------------------------------------*/ 

current_post_id = ''; 
var $projectWrapper = $("#project-wrapper"); 

eQgetProjectViaAjax = function(e) { 

var post_id = $(this).attr("data-post_id"); 
current_post_id = post_id; 
var nonce = $(this).attr("data-nonce"); 
var $prev = $('.project-link[data-post_id="' + post_id + '"]').parent().parent().prev('.portfolio-item'); 
var $next = $('.project-link[data-post_id="' + post_id + '"]').parent().parent().next('.portfolio-item'); 
var prev_item_id = ''; 
var next_item_id = ''; 

// Get the id's of previous and next projects 
if ($prev.length !== 0 && $next.length !== 0) { 
    prev_item_id = $prev.find('.project-link').attr("data-post_id"); 
    next_item_id = $next.find('.project-link').attr("data-post_id"); 
} else if ($prev.length !== 0) { 
    prev_item_id = $prev.find('.project-link').attr("data-post_id"); 
} else if ($next.length !== 0) { 
    next_item_id = $next.find('.project-link').attr("data-post_id"); 
} 

$(".single-img-loader").css('opacity', 1);  
eQcloseProject(); 

$.ajax({ 
    type : "post", 
    context: this, 
    dataType : "json", 
    url : headJS.ajaxurl, 
    data : {action: "eq_get_ajax_project", post_id : post_id, nonce: nonce, prev_post_id : prev_item_id, next_post_id : next_item_id}, 
    beforeSend: function() { 
     // Activate the overlay over the current project 
     $('.project-link[data-post_id="' + post_id + '"]').next('.blocked-project-overlay').addClass('overlay-active'); 

     if(!$.browser.opera) { 
      $.scrollTo(0, 500, { easing: 'easeOutCubic' }); 
     } else { 
      $.scrollTo(0, 300, { easing: 'easeOutCubic' }); 
     } 
    }, 
    success: function(response) { 
    $projectWrapper.html(response['html']); 
    $projectWrapper.find('#portfolio-item-meta, #single-item').css('opacity', 0); 

    $("#single-item .single-img").load(function() { 
     $(this).stop().animate({ opacity: 1 }, 300); 
     $(".single-img-loader").stop().animate({ opacity: 0 }, 300); 
    }); 
}, 
complete: function() { 
    eQopenProject(); 
    $(".prev-portfolio-post, .next-portfolio-post").click(eQgetProjectViaAjax); 
    $('.prev-portfolio-post, .next-portfolio-post').click(showLoaderImg); 
    $(".close-current-post").click(eQcloseProject);    
    $('#overlay').fadeOut(500); 
    $projectWrapper.find('#portfolio-item-meta, #single-item').stop().animate({ opacity: 1 }, 400); 
} 
}); 

我想我必須在AJAX/jQuery的莫名其妙註冊ACF但我不能找到一種方法來實現這個目標,我一直在尋找小時。

<?php get_field('project_name'); ?> 

這是我需要調用的字段。

回答

0

假設你正在使用/wp-load.php來處理你的AJAX調用(的headJS.ajaxurl在jQuery的文件中的值),所有的插件(包括ACF)將被加載,並且任何自定義字段附加到functions.php以及。您發佈的get_field()方法假定當前循環中的頁面爲$post->ID,但您可以傳入與第二個參數不同的帖子ID以覆蓋此值(如果需要)。在你的榜樣,在你的PHP文件,它應該閱讀:http://www.advancedcustomfields.com/docs/functions/get_field/

$project_name = get_field('project_name', $current_post_id); 

您可以在這裏的文檔閱讀更多

相關問題