I am trying to load the individual post information on click into the page when clicking the post title. I have managed to get my script to loop through the titles in the post array and on click load the post titles once more into a div. I want this to load the individual post information instead on click.
PHP Post Array Functions -
add_action( "wp_ajax_success_stories", "sa_get_paginated_posts" );
add_action( "wp_ajax_nopriv_success_stories", "sa_get_paginated_posts" );
function sa_get_paginated_posts(){
$args = array(
'posts_per_page' => -1,
'post_type' => 'success-stories',
'orderby' => 'post_date',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
$queryitems = $the_query->get_posts();
$alldata = array();
if ($queryitems){
foreach ($queryitems as $query_item){
$success_story = array();
$success_story["ID"] = $query_item->ID;
$success_story["title"] = $query_item->post_title;
$alldata[] = $success_story;
}
}
$encoded_data = json_encode($alldata);
echo $encoded_data;
die();
}
function sa_get_posts($post_amount, $post_type){
$args = array(
'posts_per_page' => $post_amount,
'post_type' => $post_type,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'ASC'
);
Using this function to loop through -
<? if ($success_stories): //success story posts ?>
<ul class="small-block-grid-3">
<? foreach ($success_stories as $success_story):
$title = $success_story->post_title; ?>
<li><a data-id="<? echo $success_story->ID; ?>" class="success-extra" href="#" title=""><? echo $title; ?></a></li>
<? endforeach; ?>
</ul>
<div id="success-list"></div>
<a class="success-close" href="#" title="">CLOSE</a>
<? endif; ?>
Javascript/ AJAX Call
var $json_response_box = $("#success-list");
function get_ajax_success_posts() {
//this line gets the id attribute from the link
var current_story = $(this).attr("data-id");
console.log(current_story);
jQuery.ajax({
type: "GET",
url: "WORDPRESSDIRECTORY/admin-ajax.php",
cache: true,
data: {
action: "success_stories"
},
dataType: "JSON",
success: function(data) {
var arrayLength = data.length;
for (var i = 0; i < arrayLength; i++) {
var success_id = data[i].ID;
var success_title = data[i].title;
console.log(success_id);
story = '<ul>';
story += '<li>';
story += success_title;
story += '</li>';
story += '</ul>';
$json_response_box.append(story).fadeIn(1000);
}
},
error: function(errorThrown) {
console.log("fail-posts");
}
});
}
Appreciate your suggestions,
D