我在我的函数中使用它,我可以使用它删除当前页面(称为-bucket的自定义帖子类型)及其所有子页面:
function delete_post(){
global $post;
$deletepostlink= add_query_arg( \'frontend\', \'true\', get_delete_post_link( get_the_ID() ) );
if (current_user_can(\'edit_post\', $post->ID)) {
echo \'<span><a class="post-delete-link" onclick="return confirm(\\\'Are you sure to delete?\\\')" href="\' . $deletepostlink . \'">Delete this </a></span>\';
}
}
//Redirect after delete post in frontend
add_action(\'trashed_post\',\'trash_redirection_frontend\');
function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, \'frontend\', FILTER_VALIDATE_BOOLEAN ) ) {
$args = array(
\'posts_per_page\' => -1,
\'order\'=> \'ASC\',
\'post_parent\' => $post_id,
\'post_type\' => \'bucket\'
);
// Filter through all pages and find Portfolio\'s children
$children = get_children( $args );
global $wpdb;
foreach($children as $child){
$childs[$child->ID] = $child->ID;
}
$sql = "UPDATE {$wpdb->posts} SET post_status = \'trash\' WHERE ID IN (" . implode( \', \', $childs ) . ")";
$wpdb->query($sql);
wp_redirect();
exit;
}
}
然后我把它称为我的主题
<?php echo delete_post(); ?>
当我在显示所有帖子的页面模板中使用此函数时,它显然会删除整个页面及其所有子页面,而不仅仅是通过查询/for each语句调用单个帖子
下面是我正在使用的示例模板代码以及调用我的函数的位置
<?php
/*
Template Name Posts: Bucket Overview
*/
?>
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="content">
<div class="stash-container">
<div class="main-view">
<?php
global $authordata, $post;
$my_meta = get_post_meta( $id, \'connect\', true);
$portfolio_sections = array(
\'post_type\' => \'bucket\',
\'category_name\' => \'Folder\',
\'meta_key\' => \'connect\',
\'meta_value\' => $my_meta,
\'orderby\' => \'menu_order\',
\'posts_per_page\' => -1,
\'sort_column\' => \'menu_order\',
\'sort_order\' => \'ASC\'
);
$sections = get_posts($portfolio_sections);
foreach ($sections as $section) { ?>
<ul class="things">
<div class="category"> <i class="fa fa-folder-o" aria- hidden="true"> </i> <?php echo $section->post_title; ?>
(<?php $children = get_pages( array( \'child_of\' => $section- >ID, \'post_type\' => \'bucket\',) ); echo count($children);?>)
<span class="modify-folder">
<?php echo delete_post(); ?> | edit
</span>
</div>
<?php
global $authordata, $post;
$my_meta = get_post_meta( $id, \'connect\', true);
$portfolio_sites = array(
\'post_type\' => \'bucket\',
\'category_name\' => \'drop\',
\'meta_key\' => \'connect\',
\'meta_value\' => $my_meta,
\'orderby\' => \'menu_order\',
\'posts_per_page\' => -1,
\'sort_column\' => \'menu_order\',
\'sort_order\' => \'ASC\',
\'post_parent\' => $section->ID
);
$sites = get_posts($portfolio_sites);
foreach ($sites as $site) { ?>
<li class="sites">
<a href="<?php echo get_the_permalink($site->ID); ?>">
<div class="rollover">
</div>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
因此,您可以在上面的模板中的span class-modify文件夹中看到我调用的删除链接。但是它删除了整个页面,而不仅仅是$section->ID-我已经尝试将所有引用更改为将主功能文件中的\\u ID和post\\u ID改为$section->ID,但它不起作用!
请帮忙!
谢谢