Bạn phải tạo bạn hoạt động trong chủ đề của bạn functions.php Dưới đây là ví dụ về các bài đăng tải phổ biến sử dụng ajax
function getPopularPosts()
{
$popularpostdata = wpp_get_mostpopular_data();
foreach($popularpostdata as $populardata)
{
$cur_id = $populardata->id;
$cur_date = $populardata->post_date;
$cur_date = date("F j, Y", strtotime($cur_date));
?>
<article id="<?php echo $populardata->post_slug.'_mp';?>" data-attr-post-title="<?php echo $populardata->title; ?>" <?php post_class(); ?>>
<p class="advt_disclosure"><?php echo advtdescloser(); ?></p>
<?php
echo '<h6>'.getCategoryLink().'</h6>';
$post_mp = get_post($cur_id);
?>
<h1><?php echo $populardata->title;?></h1>
<div class="entry-content">
<div class="post-details"> <?php echo getAuthorData($post_mp->post_author,$cur_id,$populardata->title,$cur_date); ?> </div>
<div class="collapsediv">
<div class="row">
<div class="col-sm-9 post_article">
<?php
$content = $populardata->postcontent; //$content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
wp_link_pages(array(
'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'twentyfifteen') . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __('Page', 'twentyfifteen') . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
));
?>
<p class="tags"> TAGS: <?php echo get_the_tag_list('', __(', ', 'twentyfifteen'))?></p>
</div>
<div class="col-sm-3 hot_deal"> <?php echo getAdvertisements(); ?> </div>
<div class="col-sm-12 comment_section"> <?php echo getPostCommentsandSocialmediasharing($post_mp->post_author,$cur_id,$populardata->title); ?> </div>
</div>
</div>
</div>
<footer class="entry-footer">
<?php //twentyfifteen_entry_meta(); ?>
<?php //edit_post_link(__('Edit', 'twentyfifteen'), '<span class="edit-link">', '</span>'); ?>
</footer>
<div class="expander">
<button class="expand" data-text-swap="Close article">expand article</button>
</div>
</article>
<?php }
exit();
}
add_action('wp_ajax_getPopularPosts', 'getPopularPosts');
add_action('wp_ajax_nopriv_getPopularPosts', 'getPopularPosts');`
And for call ajax you need to put some where in your themes footer.php
`$(".popular_posts").click(function(e) {
e.preventDefault();
if($('.popularposts').html().length==0)
{
$('.popularposts').html('<p class="text-center" style="margin-top:40px;"><img src="<?php echo get_home_url(); ?>/wp-content/themes/twentyfifteen/images/ajax-loader.gif"></p>');
$.ajax({
type: 'POST',
url: "<?php echo get_home_url(); ?>/wp-admin/admin-ajax.php",
data: {
action: 'getPopularPosts'
},
success: function(data) {
$('.popularposts').html(data);
}
});
}
});
Nguồn
2015-10-09 11:36:43
Xin chào, tôi sẽ kiểm tra điều này, cho bạn biết cách hoạt động :) –
@Chris_O Cảm ơn người đàn ông, nó thực sự giúp tôi. – Toretto