php - 从 Wordpress 注销自定义帖子类型

标签 php wordpress custom-post-type

我正在尝试删除通过 Wordpress 中的不同主题设置的自定义帖子类型,现在所有这些帖子都分配给 portfoliopost_type。经过大量搜索,我找到了下面的代码,但它似乎不起作用。我尝试将它同时添加到新主题和旧主题 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')
    )
);

最佳答案

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

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

关于php - 从 Wordpress 注销自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749843/

相关文章:

php - 使用 iOS 缓存 Web 表单

php - 在 WooCommerce 中设置最低订单金额

wordpress - 找不到自定义帖子类型自定义分类法

javascript - Node.js - 限制直接访问服务器?仅当来自其他页面并且存在表单 POST 变量时才允许/加载

PHP 获取请求阻塞

php - 从 MySQL 的字符串中解析 HTML 标签

html - 使用详细信息标签时如何将光标从 "text"更改为 "default"

html - CSS 菜单在 IE 中左对齐,但在 Chrome 中居中对齐

Wordpress 自定义分类分页不起作用