释放双眼,带上耳机,听听看~!
有时主题在提交评论后会刷新整个网页才可获取资源,别人家的都不是这样的,以下可以改善评论后无刷新即可获取资源。
在function.php文件加入以下代码
<?php define('AC_VERSION','2.0.0'); if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) { wp_die('请升级到4.4以上版本'); } if(!function_exists('fa_ajax_comment_scripts')) : function fa_ajax_comment_scripts(){ if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); } wp_enqueue_style( 'ajax-comment', get_template_directory_uri() . '/ajax-comment/app.css', array(), AC_VERSION ); wp_enqueue_script( 'ajax-comment', get_template_directory_uri() . '/ajax-comment/app.js', array( 'jquery' ), AC_VERSION , true ); wp_localize_script( 'ajax-comment', 'ajaxcomment', array( 'ajax_url' => admin_url('admin-ajax.php'), 'order' => get_option('comment_order'), 'formpostion' => 'bottom', //默认为bottom,如果你的表单在顶部则设置为top。 ) ); } endif; if(!function_exists('fa_ajax_comment_err')) : function fa_ajax_comment_err($a) { header('HTTP/1.0 500 Internal Server Error'); header('Content-Type: text/plain;charset=UTF-8'); echo $a; exit; } endif; if(!function_exists('fa_ajax_comment_callback')) : function fa_ajax_comment_callback(){ $comment = wp_handle_comment_submission( wp_unslash( $_POST ) ); if ( is_wp_error( $comment ) ) { $data = $comment->get_error_data(); if ( ! empty( $data ) ) { fa_ajax_comment_err($comment->get_error_message()); } else { exit; } } $user = wp_get_current_user(); do_action('set_comment_cookies', $comment, $user); $GLOBALS['comment'] = $comment; //根据你的评论结构自行修改,如使用默认主题则无需修改 ?> <li <?php comment_class(); ?>> <article class="comment-body"> <footer class="comment-meta"> <div class="comment-author vcard"> <?php echo get_avatar( $comment, $size = '56')?> <b class="fn"> <?php echo get_comment_author_link();?> </b> </div> <div class="comment-metadata"> <?php echo get_comment_date(); ?> </div> </footer> <div class="comment-content"> <?php comment_text(); ?> </div> </article> </li> <?php die(); } endif; add_action( 'wp_enqueue_scripts', 'fa_ajax_comment_scripts' ); add_action('wp_ajax_noprjiv_ajax_comment', 'fa_ajax_comment_callback'); add_action('wp_ajax_ajax_comment