2

I am having a issue with ACF and AJAX: the custom fields aren't getting called into the AJAX portfolio when it loads.

I am relatively new to AJAX & PHP and not sure how to fix this.

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();
?>

This is my jQuery file:

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);
}
});

I think I have to somehow register the ACF in the AJAX/jQuery but I cannot find a way to accomplish that, and I have been searching for hours.

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

This is the field I need to call.

4

1 回答 1

2

假设您正在使用/wp-load.php处理 AJAX 调用(headJS.ajaxurljQuery 文件中的值),所有插件(包括 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/

于 2012-10-14T01:28:48.137 回答