download201401 2016-03-23 16:43 采纳率: 0%
浏览 31

尾随逗号导致Wordpress自定义帖子无法按预期工作

I've got a custom post that I'm creating. It's used as a page, and you can click on the menu item to get to it. However, in the rewite subarray, I left a trailing comma, this seemed to "break" (although no errors were present) wordpresses permalink structure when set to "pretty permalinks". However when set to default, it would function just fine.

The offending code:

$args = array(
        'labels'               => $labels,
        'description'          => '',
        'public'               => true,
        'hierarchical'         => false,
        'exclude_from_search'  => true,
        'publicly_queryable'   => true,
        'show_ui'              => true,
        'show_in_menu'         => true,
        'show_in_nav_menus'    => true,
        'show_in_admin_bar'    => true,
        'menu_position'        => 21,
        'menu_icon'            => 'dashicons-id',
        'capability_type'      => 'page',
        'capabilities'         => array(),
        'map_meta_cap'         => true,
        'supports'             => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'register_meta_box_cb' => null,
        'taxonomies'           => array( 'profile-position' ),
        'has_archive'          => false,
        'rewrite'              => array( 'slug' => 'profile', ),
        'query_var'            => true,
        'can_export'           => true,
        'delete_with_user'     => false,
    );

    register_post_type( 'profile', $args );

    register_taxonomy(
        'profile-position',
        array( 'profile' ),
        array(
            'hierarchical' => false,
            'labels' => array(
                'name'                       => _x( 'Positions', 'taxonomy general name' ),
                'singular_name'              => _x( 'Position', 'taxonomy singular name' ),
                'search_items'               => __( 'Search Positions' ),
                'all_items'                  => __( 'All Positions' ),
                'edit_item'                  => __( 'Edit Position' ),
                'update_item'                => __( 'Update Position' ),
                'add_new_item'               => __( 'Add New Position' ),
                'new_item_name'              => __( 'New Position Name' ),
                'menu_name'                  => __( 'Positions' ),
                'view_item'                  => __( 'View Position' ),
                'popular_items'              => __( 'Popular Positions' ),
                'separate_items_with_commas' => __( 'Separate positions with commas' ),
                'add_or_remove_items'        => __( 'Add or remove positions' ),
                'choose_from_most_used'      => __( 'Choose from the most used positions' ),
                'not_found'                  => __( 'No positions found' )
            ),
            'rewrite' => array(
                'hierarchical' => false,
                'slug' => 'profiles/position',
                'with_front' => false
            ),
        )
    );

    register_taxonomy_for_object_type( 'profile-position', 'profile' );

The functioning code:

// Custom post type for profiles
    $args = array(
        'labels'               => $labels,
        'description'          => '',
        'public'               => true,
        'hierarchical'         => false,
        'exclude_from_search'  => true,
        'publicly_queryable'   => true,
        'show_ui'              => true,
        'show_in_menu'         => true,
        'show_in_nav_menus'    => true,
        'show_in_admin_bar'    => true,
        'menu_position'        => 21,
        'menu_icon'            => 'dashicons-id',
        'capability_type'      => 'page',
        'capabilities'         => array(),
        'map_meta_cap'         => true,
        'supports'             => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'register_meta_box_cb' => null,
        'taxonomies'           => array( 'profile-position' ),
        'has_archive'          => false,
        'rewrite'              => array( 'slug' => 'profile', 'with_front' => false ),
        'query_var'            => true,
        'can_export'           => true,
        'delete_with_user'     => false,
    );

    register_post_type( 'profile', $args );

    register_taxonomy(
        'profile-position',
        array( 'profile' ),
        array(
            'hierarchical' => false,
            'labels' => array(
                'name'                       => _x( 'Positions', 'taxonomy general name' ),
                'singular_name'              => _x( 'Position', 'taxonomy singular name' ),
                'search_items'               => __( 'Search Positions' ),
                'all_items'                  => __( 'All Positions' ),
                'edit_item'                  => __( 'Edit Position' ),
                'update_item'                => __( 'Update Position' ),
                'add_new_item'               => __( 'Add New Position' ),
                'new_item_name'              => __( 'New Position Name' ),
                'menu_name'                  => __( 'Positions' ),
                'view_item'                  => __( 'View Position' ),
                'popular_items'              => __( 'Popular Positions' ),
                'separate_items_with_commas' => __( 'Separate positions with commas' ),
                'add_or_remove_items'        => __( 'Add or remove positions' ),
                'choose_from_most_used'      => __( 'Choose from the most used positions' ),
                'not_found'                  => __( 'No positions found' )
            ),
            'rewrite' => array(
                'hierarchical' => false,
                'slug' => 'profiles/position',
                'with_front' => false
            ),
        )
    );

    register_taxonomy_for_object_type( 'profile-position', 'profile' );

the offending line: 'rewrite' => array( 'slug' => 'profile', ) the functioning line(s): 'rewrite' => array( 'slug' => 'profile') 'rewrite'=> array( 'slug' => 'profile', 'with_front' => false )

To note - once the comma is removed, with or without the second entry into the rewrite array, expected functionality returns and I can access the page. My understanding is that php ignores trailing commas and it is technically okay to do so (although I typically like to avoid it and this was an error on my part in regards to personal preference).

So what could be causing the website to return to home page when there is a trailing comma?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器