我在Wordpress网站的前端创建了一个删除帖子按钮。它会很好地删除帖子,但随后尝试再次重新加载帖子,从而导致 404。删除到特定 url 后有没有办法重定向它?这是我的代码:
function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
global $post;
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}
然后在模板上:
<?php wp_delete_post_link('Delete This', '<p>', '</p>'); ?>
我认为您正在寻找wp_redirect
函数。
$location = 'http://domainname.com/pagename/';
$status = '301';
wp_redirect( $location, $status );
exit;
只需在成功删除代码后放置即可。
在回显之后立即运行此函数怎么样:
header( 'Location: yournewURL.html' ) ;
将"您的新网址.html"替换为您需要重定向到的页面。