我们正在try 从WordPress的CPT中删除Slug. 我们确实使用Blow代码做到了这一点,但我们有数百个CPT,并在我们的子主题函数中重复代码.php看起来非常难看
`add_filter( 'post_type_link', 'cxc1_product_remove_slug', 10, 3 );
function cxc1_product_remove_slug( $post_link, $post, $leavename ) {
// product is my post type
if ( 'taller_marmo' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'cxc2_product_remove_slug', 10, 3 );
function cxc2_product_remove_slug( $post_link, $post, $leavename ) {
// product is my post type
if ( '16arlington' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'cxc3_product_remove_slug', 10, 3 );
function cxc3_product_remove_slug( $post_link, $post, $leavename ) {
// product is my post type
if ( 'alemais' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}`your text``
add_action( 'pre_get_posts', 'cxc_parse_request_function' );
function cxc_parse_request_function( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'page','taller_marmo', '16arlington', 'alemais' ) );
}
}
有没有办法将第一块代码组合到一个函数中?
有没有办法将第一块代码组合到一个函数中?