https://youtu.be/6U92B_MHfqU
复制并粘贴此视频注释中的尺寸代码。这是在wordpress中创建自定义ajax加载的最简单方法。按照步骤进行操作,最后生成自定义ajax加载更多。
step1:职位类型代码:
//start video post
$labels2 = array(
\'name\' => _x(\'video\', \'post type general name\', \'robin\'),
\'singular_name\' => _x(\'video\', \'post type singular name\', \'robin\'),
\'add_new\' => _x(\'Add New\', \'video\', \'robin\'),
\'add_new_item\' => __(\'Add New video\', \'robin\'),
\'edit_item\' => __(\'Edit video\', \'robin\'),
\'new_item\' => __(\'New video\', \'robin\'),
\'all_items\' => __(\'All video\', \'robin\'),
\'view_item\' => __(\'View video\', \'robin\'),
\'search_items\' => __(\'Search video\', \'robin\'),
\'not_found\' => __(\'No video found\', \'robin\'),
\'not_found_in_trash\' => __(\'No video found in Trash\', \'robin\'),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'video\'
);
$args2 = array(
\'labels\' => $labels2,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'taxonomies\' => array( \'category\',\'post_tag\'),
\'supports\' => array(\'title\',\'editor\',\'thumbnail\')
);
register_post_type(\'video\',$args2);
//end video post
step2:功能挂钩:
//load more ajax
/* ===============================================*/
function my_wfsubmit_callback() {
$id = $_POST[\'hide\'];
$current=$id;
$prev=2;
$prev=$prev+$current;
$i=0;
$the_query = new WP_Query(array( \'post_type\' => \'video\', \'posts_per_page\' =>\'-1\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' ));
if($the_query->have_posts()):
while($the_query->have_posts()):$the_query->the_post();?>
<?php if($i>=$current && $i<$prev){?>
<img alt="" src="<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($query->ID),\'full\');
echo $thumb[0];?>">
<?php the_title();?>
<?php the_content();?>
<?php }$i++;?>
<?php endwhile;
wp_reset_postdata();
endif;
die();
}
add_action( \'wp_ajax_wfsubmit_action\', \'my_wfsubmit_callback\' ); //wfsubmit_action = ajax action with wordpress hook wp_ajax_wfsubmit_action
add_action( \'wp_ajax_nopriv_wfsubmit_action\', \'my_wfsubmit_callback\' ); //wfsubmit_action = ajax action with wordpress hook wp_ajax_nopriv_wfsubmit_action
/* ================= end ajax submit ===============*/
step3:PHP代码:
<?php $args = array( \'post_type\' => \'video\', \'posts_per_page\' =>\'4\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' );
$query=new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
?>
<img alt="" src="<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($query->ID),\'full\');
echo $thumb[0];?>">
<?php the_title();?>
<?php endwhile;?>
<div id="sendtxt"></div>
<?php $args1 = array( \'post_type\' => \'video\', \'posts_per_page\' =>\'-1\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' );
$query1=new WP_Query($args1);
$c=0;
while ( $query1->have_posts() ) : $query1->the_post();
$c++;
endwhile;
wp_reset_postdata();
$max=$c;
?>
<form id="webform" role="form" class="webform" >
<textarea name="hide" style="display:none;" class="CUR" id="output">4
</textarea>
<a href="javascript:void(0)" id="webformsubmit" class="read-btn blck">Load More</a>
</form>
step4:脚本代码:
<script type="text/javascript" src="/wordpress/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(\'#webformsubmit\').click(function(event){
event.preventDefault();
var priBox = "extra"; //extra data,field,tag
var ajaxurlsb = "<?php echo admin_url( \'admin-ajax.php\' );?>"
$.ajax({
type: "POST",
url: ajaxurlsb+\'?action=wfsubmit_action\',
data: $("#webform").serialize()+\'&txtp=\'+priBox,
success: function(data) { // Get the result and asign to each cases
//$(\'#sendtxt\').html(data); //output div ID (sendtxt)
$(\'#sendtxt\').append(data);
}
});
});
// counter
$(\'#webformsubmit\').click(function() {
$(\'#output\').html(function(i, val) { return val*1+2 });
});
// button display
$(\'#webformsubmit\').click(function() {
var curr_val = $(\'#output\').val();
if(curr_val ><?php echo $max;?>){
$("#webformsubmit").css("display", "none");
}
});
</script>
<script type="text/javascript" src="/wordpress/js/jquery-3.1.1.min.js"></script>
step5: jquery下载链接:
https://jquery.com/download/