gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:19:27 24 4
gpt4 key购买 nike

我在使用 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)将被加载,并且 function.php 中的任何自定义字段加载项也会被加载。您发布的 get_field() 方法假定循环中页面的当前 $post->ID ,但您可以传入不同的帖子 ID 作为第二个如果需要的话可以重写这个参数。在您的示例中,在您的 PHP 文件中,它应为:

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

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

关于jquery - 使用 AJAX/jQuery 调用高级自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12244231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com