使用Register_post_type()修改现有的帖子类型

时间:2012-09-13 作者:mrwweb

有很多情况下,主题或插件注册了一个帖子类型,而您想要修改它。当然有add_post_type_support()remove_post_type_support(), 但这些并不能提供完整的论点列表register_post_type() 需要。特别是,我可能想禁用帖子类型存档、隐藏管理UI、隐藏搜索等,而不使用其他帖子类型设置。

这个Codex page for register_post_type() 把这个挂在我面前:

说明创建或修改帖子类型。

但在过去,当我尝试这样做时,它似乎不起作用。这个函数真的是用来修改帖子类型的吗?如果是这样的话,你能简单地重新声明几个参数而不去理会其余的参数吗?

看到这里isn\'t even a deregister_post_type() function, 我不明白怎么才能做到。

6 个回复
最合适的回答,由SO网友:Otto 整理而成

此功能真的用于修改帖子类型吗

如果是这样的话,你能简单地重新声明几个论点,而不去管其他的吗?

否。如果要将参数修改为post类型,则需要使用get_post_type_object 要获取post类型对象,请修改其中所需的内容,然后使用修改后的类型作为新的$args参数重新注册它。

SO网友:Radley Sustaire

经过一些研究,我发现这些答案都不是最新的。

截至2015年12月8日,WordPress包括一个新的过滤器,register_post_type_args, 它允许您钩住已注册post类型的参数。

function wp1482371_custom_post_type_args( $args, $post_type ) {
    if ( $post_type == "animal-species" ) {
        $args[\'rewrite\'] = array(
            \'slug\' => \'animal\'
        );
    }

    return $args;
}
add_filter( \'register_post_type_args\', \'wp1482371_custom_post_type_args\', 20, 2 );

SO网友:jjeaton

下面是一个如何使用\'registered_post_type\' 筛选以修改其他插件中的帖子类型。

我使用的插件在其定义中没有包含menu\\u图标,所以我想添加一个自己的插件。

<?php
/**
 * Add a menu icon to the WP-VeriteCo Timeline CPT
 *
 * The timeline plugin doesn\'t have a menu icon, so we hook into \'registered_post_type\'
 * and add our own.
 *
 * @param  string $post_type the name of the post type
 * @param  object $args the post type args
 */
function wpse_65075_modify_timeline_menu_icon( $post_type, $args ) {
    // Make sure we\'re only editing the post type we want
    if ( \'timeline\' != $post_type )
        return;

    // Set menu icon
    $args->menu_icon = get_stylesheet_directory_uri() . \'/img/admin/menu-timeline.png\';

    // Modify post type object
    global $wp_post_types;
    $wp_post_types[$post_type] = $args;
}
add_action( \'registered_post_type\', \'wpse_65075_modify_timeline_menu_icon\', 10, 2 );

SO网友:fuxia

钩入\'registered_post_type\' 在其他代码注册之后。它在register_post_type(). 有两个参数:$post_type$args.
现在您可以更改此帖子类型的任何内容。检查$GLOBALS[\'wp_post_types\'] 对于一些选项。

SO网友:Abouasy

我也面临同样的问题The Events Calendar 插件。

我为函数添加了以下代码。php修改tribe_organizer 岗位类型

function tribe_modify_organizer() {
 //New arguments
    $tribe_organizer_args = get_post_type_object(\'tribe_organizer\'); // get the post type to modify
    $tribe_organizer_args-> taxonomies = array(\'post_tag\' , \'tribe_events_cat\'); // add taxonomies support
    $tribe_organizer_args-> exclude_from_search = false; // show in search result
 //re-register the same post type includeing the new args
    register_post_type( \'tribe_organizer\', $tribe_organizer_args );
}
add_action( \'init\', \'tribe_modify_organizer\', 100 );

SO网友:Jonas Lundman

我不知道这是否丑陋,但你可以改变GLOBAL 当您需要操纵单个参数时,可以使用占位符“动态”。这就是我们如何使用非公开帖子类型contents 在管理菜单中可接受。我们在菜单渲染之前钩住close,在菜单渲染之后钩住close:

function entex_theme_make_contents_public(){
    $GLOBALS[\'wp_post_types\'][\'contents\']->public = true;
}
add_action(\'admin_menu\', \'entex_theme_make_contents_public\', 10);

function entex_theme_make_contents_private_again(){
    $GLOBALS[\'wp_post_types\'][\'contents\']->public = \'\';
}
add_action(\'admin_menu\', \'entex_theme_make_contents_private_again\', 12);
在我们的例子中,我们希望管理菜单帖子列表插件接受我们的帖子类型,正如他们所调用的return get_post_types(array(\'public\' => true)); 在他们的钩子里,优先级为11。。。

开发人员-如果这可能导致任何问题,请发表评论。

结束

相关推荐

Grouping post-types in loop

当用户从下拉列表中选择特定类别时,将显示多个帖子类型的列表。我想用标题中的帖子类型标题分组显示数据。我不确定如何划分为岗位类型组。我花了一点时间寻找,但没有找到我真正需要的东西。我还试图添加<?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> 但这只是重复(显然)。调用循环<div id=\"content\" role=\"main\" style=\"float:right; width:765px