在单独的函数中修改自定义POST类型重写规则

时间:2012-05-30 作者:HandiworkNYC.com

我想修改自定义帖子类型的重写规则,tribe_events, 在“事件日历”核心插件文件(The-Events-Calendar.class第24行)中注册为post类型:

    protected $postTypeArgs = array(
        \'public\' => true,
        \'rewrite\' => array(\'slug\' => \'event\', \'with_front\' => false),
        \'menu_position\' => 6,
        \'supports\' => array(\'title\',\'editor\',\'excerpt\',\'author\',\'thumbnail\', \'custom-fields\'),
        \'capability_type\' => array(\'tribe_event\', \'tribe_events\'),
        \'map_meta_cap\' => true
    );
我想做的是修改行:

        \'rewrite\' => array(\'slug\' => \'event\', \'with_front\' => false)
收件人:

\'重写\'=>数组(\'slug\'=>\'事件/%lugares%\',\'with\\u front\'=>false)

其中,“%lugares%”是自定义分类法的名称。

然后,我的计划是使用以下函数来完成重写过程:

add_filter(\'post_type_link\', \'events_permalink_structure\', 10, 4);
function events_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, \'%lugares%\' ) ) {
        $lugares_term = get_the_terms( $post->ID, \'lugares\' );
        $post_link = str_replace( \'%lugares%\', array_pop( $lugares_term )->slug, $post_link );
    }
    return $post_link;
}
问题是我不想修改核心插件文件。是否有一种方法可以在我的主题函数中的单独函数中修改自定义post类型重写参数。php??

谢谢

1 个回复
SO网友:shea

是的,我相信你可以。将此代码粘贴到主题的functions.php 文件:

function change_tribe_events_rewrite_rules() {
    global $wp_post_types;
    $rewrite = &$wp_post_types[\'tribe_events\']->rewrite;
    $rewrite[\'slug\'] = \'event/%lugares%\';
}
add_action( \'init\', \'change_tribe_events_rewrite_rules\', 999 );

结束

相关推荐

Custom post type permalinks

我有一个名为“数字”的自定义帖子类型,我想实现URL的“帖子”如下http://website.com/post-name 而不是http://website.com/number/post-name.我试图将register\\u post\\u type中的“rewrite”参数设置为:\'rewrite\' => array( \'slug\' => \'\', \'with_front\' => false ), 但它没有起作用。即使我保存了永久链接设置以刷新它。(我将永久链接设