Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AJAX/jQuery调用高级自定义字段_Jquery_Ajax_Wordpress_Custom Fields - Fatal编程技术网

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

使用AJAX/jQuery调用高级自定义字段,jquery,ajax,wordpress,custom-fields,Jquery,Ajax,Wordpress,Custom Fields,我对ACF和AJAX有一个问题:当它加载时,自定义字段没有被调用到AJAX组合中 我对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 (

我对ACF和AJAX有一个问题:当它加载时,自定义字段没有被调用到AJAX组合中

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


这是我需要调用的字段。

假设您使用
/wp load.php
来处理AJAX调用(jQuery文件中
headJS.ajaxurl
的值),所有插件(包括ACF)都将被加载,functions.php中的任何自定义字段加载项也将被加载。发布时的
get_field()
方法假定循环中某个页面的当前
$post->ID
,但如果需要,可以传入另一个post ID作为第二个参数来覆盖它。在您的示例中,在PHP文件中,它应该是:

$project_name = get_field('project_name', $current_post_id);
您可以在此处的文档中阅读更多内容: