WordPress 后台文章列表中,可以单独将某篇文章移动到回收站中,但移动后页面会刷新一次,可通过下面的代码实现无刷新将文章移动到回收站中。
第一步
在当前主题目录新建一个 movepost.js 文件,将下面 JS 代码复制进去。
- jQuery(function($){
- $('body.post-type-post .row-actions .trash a').click(function( event ){
- event.preventDefault();
- var url = new URL( $(this).attr('href') ),
- nonce = url.searchParams.get('_wpnonce'), // MUST for security checks
- row = $(this).closest('tr'),
- postID = url.searchParams.get('post'),
- postTitle = row.find('.row-title').text();
- row.css('background-color','#ffafaf').fadeOut(300, function(){
- row.removeAttr('style').HTML('<td colspan="5">文章 <strong>' + postTitle + '</strong> 已移至回收站</td>').show();
- });
- $.ajax({
- method:'POST',
- url: ajaxurl,
- data: {
- 'action' : 'moveposttotrash',
- 'post_id' : postID,
- '_wpnonce' : nonce
- }
- });
- });
- });
如果是在自定义文章类型中使用,修改其中的 post-type-post 为 post-type-文章类型名称。
第二步
将下面代码添加到当前主题函数模板 functions.php 中:
- add_action( 'admin_head', 'moveposttotrash_script' );
- function moveposttotrash_script() {
- wp_enqueue_script( 'movepost', get_stylesheet_directory_uri() . '/movepost.js', array( 'jquery' ) );
- }
- add_action('wp_ajax_moveposttotrash', function() {
- check_ajax_referer( 'trash-post_' . $_POST['post_id'] );
- wp_trash_post( $_POST['post_id'] );
- die();
- });
之后,在 WordPress 后台文章列表中,点击“移动至回收站”,即可看到效果。
- 1、本站大部分内容均收集于网络,若内容侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn,我们将第一时间处理!
- 2、本站资源所需价格并非资源售卖价格,是收集、整理、编辑以及本站运营的适当补贴,并且本站不提供任何免费技术服务。
- 3、所有资源仅限参考和学习,版权归原作者所有,更多请阅读无言说网络服务器协议。