Hey,
I'm trying to load posts on wordpress using Ajax.
I have this in my functions.php file:
add_action ("wp_enqueue_scripts", "theme_register_scripts", 1);
function theme_register_scripts() {
//echo get_template_directory_uri();
wp_register_script("functions-js",esc_url(trailingslashit(get_template_directory_uri()) . "functions.js"), array("jquery"),time(),true);
$php_array = array("admin_ajax" => admin_url("admin-ajax.php"));
echo $php_array[admin_ajax];
wp_localize_script("functions-js","php_array",$php_array);
}
add_action("wp_enqueue_scripts", "theme_enqueue_scripts");
function theme_enqueue_scripts() {
wp_enqueue_script("functions-js");
}
add_action("wp_ajax_single", "get_single");
add_action("wp_admin_nopriv_single", "get_single");
function get_single() {
global $post;
echo $post;
$post_id = $_REQUEST['id'];
if($post_id) {
$post = get_post($post_id);
setup_postdata($post);
get_template_part("content", "ajax");
die();
}
}
I have this in my content-ajax file (a new file I made, referenced from the code above)
<?php
the_title();
the_content();
?>
I have this in a functions.js file I made
jQuery(document).ready( function($) {
$(document).on( 'click', 'li.bullet-inline a', function( event ) {
event.preventDefault();
var clickedID = $(this).attr('id');
alert("id val: " + clickedID)
$.ajax({
cache: false,
timeout: 8000,
url: $php_array.admin_ajax,
type: 'POST',
data: ({action:'single', id:clickedID}),
success: function(data) {
alert(data);
$( '.popup' ).html(data);
$(".popup").show();
}
})
})
})
The success Ajax use to run and the code used to work, but now it does't for some reason.
The success isn't running, I believe the $php_array.admin_ajax isn't acknowledging the code set in the php file.