gpt4 book ai didi

ajax - 引用错误 : ajax_object is not defined when loading Wordpress post via Ajax

转载 作者:行者123 更新时间:2023-12-01 19:35:52 25 4
gpt4 key购买 nike

我一直在尝试在此线程中实现@SagiveSEO 的建议:
Proper way to load a single post via Ajax?

想法:单击一个按钮并通过 AJAX 加载帖子。这个想法是你将拥有一棵按钮树,让人们可以快速导航到有用的内容。

不幸的是,它失败了。

在控制台中,我收到消息“ReferenceError: ajax_object is not defined”,它指的是行“$.post(ajax_object.ajaxurl...”

我究竟做错了什么?

这是我的 HTML:

<button class="get_project" data-postid="3300">PROJECT NAME</button>
<div class="postcontainer"></div>

这是我的 Javascript:
    jQuery(function($){
$('.get_project').click(function() {
var postid = $(this).data('postid'); // Amended by @dingo_d
$.post(ajax_object.ajaxurl, {
action: 'my_load_ajax_content ',
postid: postid
}, function(data) {
var $response = $(data);
var postdata = $response.filter('#postdata').html();
$('.postcontainer').html(postdata);
});

})
//alert( "hello world" );
});

这是我的functions.php文件中的php:
function my_load_ajax_content () {

$pid = intval($_POST['post_id']);
$the_query = new WP_Query(array('p' => $pid));

if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) {
$the_query->the_post();

$data = '
<div class="post-container">
<div id="project-content">
<h1 class="entry-title">'.get_the_title().'</h1>
<div class="entry-content">'.get_the_content().'</div>
</div>
</div>
';

}
}
else {
echo '<div id="postdata">'.__('Didnt find anything', THEME_NAME).'</div>';
}
wp_reset_postdata();


echo '<div id="postdata">'.$data.'</div>';
}

// Next two lines corrected - thanks @dingo_d
add_action ( 'wp_ajax_my_load_ajax_content', 'my_load_ajax_content' );
add_action ( 'wp_ajax_nopriv_my_load_ajax_content', 'my_load_ajax_content' );

在脚本入队函数中的 functions.php 中也需要:
wp_enqueue_script( 'myajaxpostloader',  get_template_directory_uri().'/js/ajax.js', array( 'jquery' ), '1.0', true );

wp_localize_script( 'myajaxpostloader', 'ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));

(请注意,我的 Javascript 保存为/js/ajax.js,其中/js/是 Wordpress 主题的子目录)。

最佳答案

您没有本地化您的 ajax 对象。在 25 个主题中你会这样做 - 在 functions.php你会放

wp_localize_script( 'twentyfifteen-script', 'ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
));

您的脚本排队的位置。

在你的主题中一定要使用正确的句柄 - 而不是 'twentyfifteen-script'把那个放在你的ajax代码所在的地方。所以如果你的ajax JavaScript位于一个名为 custom.js的文件中,并且您已经使用句柄 custom_js 将该脚本加入队列,然后你会放
wp_localize_script( 'custom_js', 'ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
));

在您将该脚本加入队列之后。总而言之在您的 functions.php你会这样写:
add_action('after_setup_theme', 'mytheme_theme_setup');

if ( ! function_exists( 'mytheme_theme_setup' ) ){
function mytheme_theme_setup(){
add_action( 'wp_enqueue_scripts', 'mytheme_scripts');
}
}

if ( ! function_exists( 'mytheme_scripts' ) ){
function mytheme_scripts() {
wp_enqueue_script( 'custom_js', get_template_directory_uri().'/js/custom.js', array( 'jquery'), '1.0.0', true );
wp_localize_script( 'custom_js', 'ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
));
}
}

或者至少寻找这样的代码并将本地化放在那里:)

关于ajax - 引用错误 : ajax_object is not defined when loading Wordpress post via Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39017791/

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