好了,伙计们,我都快疯了。。。
我希望将文章的内容和自定义字段附加到页面上的div中。内容和自定义字段的ID由用户选择的链接上的数据属性定义。
问题是没有填充ajax调用的函数的输出,而是复制了当前页面的整个html。
This is the html of the selectors and output div on the page...
<p><a class="funnel-direction" data-funnel-id="123" href="#">Option One</a> <a class="funnel-direction" data-funnel-id="456" href="#">Option Two</a></p>
<div id="result"></div> <!-- whole page gets duplicated in here - wtf? -->
This is enqueuing my script for the ajax call
add_action( \'wp_enqueue_scripts\', \'add_ajax_scripts\' );
function add_ajax_scripts() {
wp_enqueue_script( \'funnel-ajax\', DNA_FU . \'js/funnel-ajax.js\', array(), \'1.0.0\', true ); // working fine
wp_localize_script( \'funnel-ajax\', \'funnel_ajax\', array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
\'ajaxnonce\' => wp_create_nonce( \'ajax_post_validation\' )
));
}
This is the function being called by ajax - it is this content I want to populate in <div id="result"></div>
.
add_action ( \'wp_ajax_nopriv_dna_add_funnel_part\', \'dna_add_funnel_part\' );
add_action ( \'wp_ajax_dna_add_funnel_part\', \'dna_add_funnel_part\' );
function dna_add_funnel_part() {
$id = $_POST[\'postid\'];
$meta = get_post_meta($id, \'dna_funnel_info\', true);
$content = apply_filters(\'the_content\', get_post_field(\'post_content\', $id));
$data = !empty($meta) ? \'<p>\' . $meta . \'</p>\' : \'\';
echo $content . $data;
wp_die();
}
finally, here is my ajax .js
jQuery(document).ready(function($) {
$(\'a.funnel-direction\').on( \'click\', function(e) {
e.preventDefault();
var postid = $(this).attr(\'data-funnel-id\');
console.log(postid); // working
$.ajax({
type: \'POST\',
url: funnel_ajax.ajax_url,
data: {
action: \'dna_add_funnel_part\',
postid: postid,
},
success: function(data) {
// console.log(data);
$(\'#result\').html(data); // This populates the entire html of the current
// page - it should only popualte the output of
// the dna_add_funnel_part() wordpress function
},
fail: {
// to do ...
}
});
return false;
});
});
已经看了一遍又一遍,这不能发现任何东西,希望你们都能发现!
提前感谢
最合适的回答,由SO网友:Tom J Nowell 整理而成
First, 你犯了一个非常非常愚蠢的错误:
\'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
.....
url: funnel_ajax.ajax_url,
ajaxurl
!=
ajax_url
. 因此,您的管理AJAX请求没有返回完整的HTML页面。你甚至没有点击AJAX文件!
However, this is still completely the wrong way to go about this
我打算建议您改用REST API,它更简单、更简单,并且可以使用
register_rest_route
为您提供一个漂亮的URL,如
example.com/wp-json/richerimage/v1/dnafunnel
, 但你不需要。
事实上,您甚至不需要AJAX处理程序或端点。只要使用core给你的那一个。
首先,注册post meta,使其出现在REST API响应中:
PHP:
$args = array(
\'type\' => \'string\',
\'description\' => \'DNA Funnel Info thing\',
\'single\' => true,
\'show_in_rest\' => true,
);
register_post_meta( \'post\', \'dna_funnel_info\', $args );
然后检索所需的帖子,例如。
example.com/wp-json/wp/v2/post/123
其中123是您想要的帖子的ID。JSON响应将包含完全呈现形式的内容、标题、分类法等,包括一个包含您注册的所有元键的元节。
如果您的帖子ID用于CPT,请确保show_in_rest
注册时设置为true,并替换/post/
与该CPT的相关端点
或者更好地使用PHP:
add_filter( \'the_content\', function( $content ) {
// append post 123\'s content to the end
return $content . get_the_content( \'\', \'\', 123 );
} );