更改自定义发布类型的辅助信息

时间:2015-06-10 作者:Ryan

我使用一个插件创建一个自定义的帖子类型,其中包含一个我想要更改的slug。我不想覆盖插件文件,所以我想知道如何使用单独的函数更改slug?谢谢

// Register post type
add_action( \'init\', \'ctc_register_post_type_item\' );
function ctc_register_post_type_item() {
    // Arguments
    $args = array(
        \'labels\' => array(
                \'name\'                  => _x( \'items\', \'post type general name\', \'church-theme-content\' ),
                \'singular_name\'         => _x( \'item\', \'post type singular name\', \'church-theme-content\' ),
                \'add_new\'               => _x( \'Add New\', \'item\', \'church-theme-content\' ),
                \'add_new_item\'          => __( \'Add Item\', \'church-theme-content\' ),
                \'edit_item\'             => __( \'Edit Item\', \'church-theme-content\' ),
                \'new_item\'              => __( \'New Item\', \'church-theme-content\' ),
                \'all_items\'             => __( \'All Items\', \'church-theme-content\' ),
                \'view_item\'             => __( \'View Item\', \'church-theme-content\' ),
                \'search_items\'          => __( \'Search Items\', \'church-theme-content\' ),
                \'not_found\'             => __( \'No items found\', \'church-theme-content\' ),
                \'not_found_in_trash\'    => __( \'No items found in Trash\', \'church-theme-content\' )
        ),
        \'public\'        => ctc_feature_supported( \'items\' ),
        \'has_archive\'   => ctc_feature_supported( \'items\' ),
        \'rewrite\'       => array(
                \'slug\'          => \'items\',
                \'with_front\'    => false,
                \'feeds\'         => ctc_feature_supported( \'items\' )
        ),
        \'supports\'      => array( \'title\', \'editor\', \'excerpt\', \'publicize\', \'thumbnail\', \'comments\', \'author\', \'revisions\' ), // \'editor\' required for media upload button (see Meta Boxes note below about hiding)
        \'taxonomies\'    => array( \'ctc_item_topic\', \'ctc_item_book\', \'ctc_item_series\', \'ctc_item_speaker\', \'ctc_item_tag\' ),
        \'menu_icon\'     => \'dashicons-video-alt3\'
    );

    $args = apply_filters( \'ctc_post_type_item_args\', $args ); // allow filtering

    // Registration
    register_post_type(
        \'ctc_item\',
        $args
    );
}

2 个回复
SO网友:JHoffmann

在您发布的代码中,过滤器ctc_post_type_item_args 应用于$args 数组,然后将其传递到register_post_type().

在主题中加入功能functions.php 把它挂在过滤器上应该可以做到:

function wpse_191003_ctc_post_type_item_args( $args ) {
    $args[\'rewrite\'][\'slug\'] = "your_new_slug";
    return $args;
}
add_filter( \'ctc_post_type_item_args\', \'wpse_191003_ctc_post_type_item_args\' );

SO网友:Evan Yeung

看看Tom\'s answer 关于一个类似的问题。他钩住一个动作,向数据库中添加一个重写规则,这样就可以通过两个slug访问帖子。他实际上无法重新声明slug,但他展示了如何隐藏旧slug,使新slug看起来是正确的。

另一个option suggested by Dustin 就是打电话register_post_type 使用相同的$post\\u类型,但将slug设置为所需的类型。这似乎对他有用,但没有人证实这是否会带来问题。

结束

相关推荐

Rewrite slug for CPT

我制作并注册了一个名为“通用”的CPT。当我在菜单中设置CPT页面时,地址是www.sitename.com/generic/pagename 我想让这个CPT的slugwww.sitename.com/pagename 我试过了\'rewrite\' => array(\'slug\' => \'\'), 注册帖子时,但这没有效果。我也试过了\'rewrite\' => array(\'slug\' => get_site_url()),