从URL WordPress中删除CPT插件

时间:2020-06-24 作者:Sudhakar SJ

我已经用slug创建了自定义的post类型(例如:loan officer)。我想从URL中删除CPT slug。我已经使用了下面的代码,我见过这么多这样的线程,但仍然没有任何工作。

function custom_post() {
register_post_type(
    \'loan_officers\', array(
      \'labels\' => array(\'name\' => __( \'Loan Officers\' ), \'singular_name\' => __( \'Loan Officer\' ) ),
      \'public\' => true,
      \'has_archive\' => true,
      \'menu_icon\' => \'dashicons-calendar-alt\',
      \'supports\' => array(\'title\', \'editor\', \'thumbnail\',  \'page-attributes\'),
      \'publicly_queryable\' => true,  // you should be able to query it
      \'show_ui\' => true,  // you should be able to edit it in wp-admin
      \'show_in_menu\' => true,
      \'show_in_admin_bar\' => true,
      \'exclude_from_search\' => false,  // you should exclude it from search results
      \'show_in_nav_menus\' => true,  // you shouldn\'t be able to add it to menus
      \'has_archive\' => false,  // it shouldn\'t have archive page
      \'rewrite\' => array(
        \'slug\' => \'loan-officer\',
        \'with_front\'  => false,
      )
    )
);
  }
 add_action( \'init\', \'custom_post\');

function gp_remove_cpt_slug( $post_link, $post ) {
if ( \'loan_officers\' === $post->post_type && \'publish\' === $post->post_status ) {
    $post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );
}
return $post_link;
}
add_filter( \'post_type_link\', \'gp_remove_cpt_slug\', 10, 2 );


function gp_add_cpt_post_names_to_main_query( $query ) {
if ( ! $query->is_main_query() ) {
    return;
}
if ( ! isset( $query->query[\'page\'] ) || 2 !== count( $query->query ) ) {
    return;
}
if ( empty( $query->query[\'name\'] ) ) {
    return;
}
$query->set( \'post_type\', array( \'post\', \'page\', \'loan_officers\' ) );
}
add_action( \'pre_get_posts\', \'gp_add_cpt_post_names_to_main_query\' );
尽管如此,当我试图从管理员那里查看时,slug还是出现在URL中,没有slug也能正常工作。有人能建议如何从

领域com/贷款官/域测试。com/测试

谢谢

1 个回复
SO网友:Luke

根据我链接到的问题中提供的答案,您最好使用一个插件,例如Permalink Manager Lite. 它负责为您重定向旧URL,并管理复制问题。

唯一的代码选项是this answer 但这取决于您想自己编写多少代码。

相关推荐