首页 文章

从Wordpress取消注册自定义帖子类型

提问于
浏览
3

我正在尝试删除通过Wordpress中的其他主题设置的自定义帖子类型,现在所有这些帖子都分配给了 post_type portfolio . 经过大量的搜索,我找到了下面的代码,但它似乎没有用 . 我尝试将它添加到新主题和旧主题 functions.php .

我想删除post_type并将帖子分类并显示为普通帖子 . 我认为我正在做的是正确的,但似乎无法让它工作 - 我已经发布了自定义帖子类型的代码和取消注册分配给它的帖子的代码 .

用于注销帖子类型的代码

if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type() {
global $wp_post_types;
if ( isset( $wp_post_types[ 'portfolio' ] ) ) {
    unset( $wp_post_types[ 'portfolio' ] );
    return true;
}
return false;
}
endif;

add_action('init', 'unregister_post_type');

注册帖子类型的代码

register_post_type( 'portfolio',
    array(
        'labels' => array(
             'name' => __('Portfolio Items'),
             'singular_name' => __('Portfolio Item'),
             'add_new_item' => __('Add New Portfolio Item'),
             'edit_item' => __('Edit Portfolio Item'),
             'new_item' => __('New Portfolio Item'),
             'view_item' => __('View Portfolio Item'),
             'search_items' => __('Search Portfolio Items'),
             'not_found' => __('No portfolio items found'),
             'not_found_in_trash' => __('No portfolio items found in Trash')
        ),
        'public' => true,
        'show_ui' => true,
        'hierarchical' => false,
        'menu_position' => 7,
        //'rewrite' => array('slug' => 'portfolio'),
        'rewrite' => true,
        '_built_in' => false,
        'taxonomies' => array( 'post_tag','category','portfolio_tag', 'portfolio_category', 'client'),
        'supports' => array( 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions')
    )
);

3 回答

  • 0

    我能够使用以下代码在WordPress 4.6.1中删除它:

    function delete_post_type(){
      unregister_post_type( 'portfolio' );
    }
    add_action('init','delete_post_type', 100);
    
  • 2

    你的代码看起来不错!但是如果你取消注册post_type ......它中的帖子就会消失......所以不要过早注销它 . 在取消注册帖子类型之前,将帖子从post_type迁移到普通帖子 . 这个插件很方便:https://wordpress.org/plugins/post-type-switcher/

    但是,如果您不想将视频帖子迁移到默认帖子......您必须修改您的循环以包含这些组合类型的帖子:

    function add_custom_post_type_to_query( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'post_type', array('post', 'portfolio') );
        }
    }
    add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
    

    在使用自定义帖子类型时,不要忘记访问永久链接页面,以使WordPress识别您所做的更改 .

  • 5

    试试这段代码(优先级很高)

    function custom_unregister_theme_post_types() {
        global $wp_post_types;
    
          if ( isset( $wp_post_types["portfolio"] ) ) {
             unset( $wp_post_types[ "portfolio" ] ); //UPDATED
          }
    
    }
    add_action( 'init', 'custom_unregister_theme_post_types', 20 );
    

    注意:确保已注册的帖子类型名称是 portfolioportfolios (带s),register_post_type( $post_type ,$ args)

    UPDATED: unset( $wp_post_types[ "portfolio" ] ); //UPDATED

相关问题