我创建了一个服装岗位类型
我在前台创建了一个删除帖子的按钮,但它将我重定向到主页而不删除帖子(只有当我有管理员角色时才能删除帖子),我想让帖子的所有者也能删除它。
这是我的代码
<?php if( is_user_logged_in() && is_author(get_current_user_id()) )
echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&post=$id", 'delete-post_' . $post->ID) . "'><i class=\"fa fa-fw fa-times\"></i>حذف </a>";?>
在WordPress中添加以下代码可以让你从前端删除帖子。
方法一:-
<?php
$url = get_bloginfo('url');
if(is_user_logged_in() && is_author(get_current_user_id() && current_user_can('edit_post', $post->ID))){
echo '<a class="delete-post" href="';
echo wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID);
echo '"><i class=\"fa fa-fw fa-times\"></i>حذف</a>';
}
?>
方法2:-
<?php
if(is_user_logged_in() && is_author(get_current_user_id())){
echo '<a href="'.get_delete_post_link($post->ID).'"><i class=\"fa fa-fw fa-times\"></i>حذف</a>';
}
?>
方法一:- 方法二:- 完成。
我做了一些研究,我发现像这样的东西
//function to print publish button
function show_publish_button(){
Global $post;
//only print fi admin
echo '<form id="myForm" name="front_end_publish" method="POST" action="">
<input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
<button type="submit" name="submit" id="submit" value="حذف" class="btn" style="margin-left:2px;background:#f5f5f5;"><i class="fa fa-fw fa-times"></i>حذف</button>
</form>';}
然后:
//function to update post status
function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = $status;
wp_update_post($current_post);
}
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'trash');
}
将以上代码粘贴到function.php中
然后在模板文件中调用函数show_publish_button();来显示删除按钮。