使用AJAX / jQuery调用高级自定义字段

时间:2012-09-03 08:18:24

标签: jquery ajax wordpress custom-fields

我遇到了ACF和AJAX的问题:自定义字段在加载时不会被调用到AJAX产品组合中。

我对AJAX& amp; 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'); ?>

这是我需要调用的字段。

1 个答案:

答案 0 :(得分:2)

假设您使用/wp-load.php处理AJAX调用(jQuery文件中headJS.ajaxurl的值),将加载所有插件(包括ACF),并添加任何自定义字段在你的functions.php中也是如此。您发布的get_field()方法假定循环中页面的当前$post->ID,但您可以传入不同的帖子ID作为第二个参数,以便根据需要覆盖它。在您的示例中,在您的PHP文件中,它应显示为:

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

您可以在此处的文档中阅读更多内容:http://www.advancedcustomfields.com/docs/functions/get_field/