Created
November 18, 2014 21:12
-
-
Save mapsam/113301ec4263321c5a10 to your computer and use it in GitHub Desktop.
wordpress return post data on click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($) { | |
$('.post').on('click', function(){ | |
getPostInfo($(this).attr('data-postid')); | |
}); | |
}); | |
function getPostInfo(id) { | |
jQuery.ajax({ | |
url: ajaxurl, | |
dataType: 'json', | |
data: { | |
action: 'postdata', | |
data: id | |
}, | |
type: 'GET', | |
success: returnData | |
}); | |
} | |
function returnData(res) { | |
console.log(res); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_ajax_postdata', 'prefix_ajax_postdata' ); | |
add_action( 'wp_ajax_nopriv_postdata', 'prefix_ajax_postdata' ); | |
function prefix_ajax_postdata() { | |
$postID = intval($_GET['data']); | |
$postdata = get_post($postID); | |
echo json_encode($postdata); | |
die(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript">var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";</script> | |
<?php | |
$posts = get_posts(); | |
foreach ($posts as $p) : | |
echo '<article class="post" id="'.$p->post_name.'" data-postid="'.$p->ID.'">.$p->post_title.'</article>'; | |
endforeach; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment