cra*_*aig 1 php wordpress redirect
我创建了新的自定义帖子类型-在wordpress中进行交易,并在发布新交易帖子后将其重定向到edit.php(帖子列表)。因此,我想将其重定向到交易列表页面(http://mydemo.com/demo/edit.php?post_type=deals),所以请帮助我。
我尝试了这段代码,但是它将所有帖子类型重定向到指定页面。
add_filter( 'redirect_post_location', 'wpse_124132_redirect_post_location' );
/**
* Redirect to the edit.php on post save or publish.
*/
function wpse_124132_redirect_post_location( $location ) {
if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
return admin_url( "edit.php?post_type=deals" );
return $location;
}
Run Code Online (Sandbox Code Playgroud)
您需要使用get_post_type()函数。
将您的代码更改为
function wpse_124132_redirect_post_location( $location ) {
if ( 'deals' == get_post_type() ) {
/* Custom code for 'deals' post type. */
if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
return admin_url( "edit.php?post_type=deals" );
}
return $location;
}
Run Code Online (Sandbox Code Playgroud)
编辑:由于您get_post_type()在The Loop之外使用,您需要传递帖子的ID,
get_post_type($_POST['id'])