为什么WordPress会在自定义帖子类型的查询字符串中使用‘附件’?

时间:2013-09-02 作者:bode well carapace

为什么Wordpress会在自定义帖子类型的查询字符串中使用“附件”?

虽然我有一个稍微扭曲的结构,但我似乎找不到“依恋”在哪里起作用以及如何改变这种行为。

我的自定义帖子类型“fb\\U entry”具有自定义分类“issue”。“fb\\U entry”中的帖子会用slug“journal/%issue%”重写,其中%issue%是术语名称。我还有第二个自定义帖子类型“fb\\U issue”,它的帖子会用slug“journal”重写。还有一页写着slug“journal”。

以下是其工作原理:

用户访问/日志

  • 用户浏览FBU发布的帖子列表并进行选择
  • 用户被带到/日志/FBU发布
  • 在这里,用户浏览FBU条目中的帖子列表,其中的“发布”术语与FBU发布slug匹配,用户有URL/journal/issue-term-name/fb\\u entry-post-name这一切似乎都很好,直到最后一步用户到达404页面。调试工具栏告诉我请求是:

    但查询字符串为:

    然而,我的任何查询都不需要附件。

    EDIT

    这是投递类型注册。标准,我认为:

    按照标准,我认为:

    /* Create the \'Entry\' post type */
    function create_fb_entry(){
        register_post_type( \'fb_entry\',
            array(
                \'labels\' => array(
                    \'name\' => \'Entries\',
                    \'singular_name\' => \'Entry\',
                    \'add_new\' => \'Add New Entry\',
                    \'add_new_item\' => \'Add New Entry\',
                    \'edit_item\' => \'Edit Entry\',
                    \'new_item\' => \'New Entry\'
                ),
                \'public\' => true,
                \'rewrite\' => array(
                    \'slug\' => \'journal/%issue%\',
                    \'with_front\'  => false,
                ),
                \'supports\' => array(
                    \'title\',
                    \'editor\',
                    \'excerpt\',
                    \'page-attributes\',
                    \'revisions\',
                    \'thumbnail\'
                )
            )
        );
    }
    add_action(\'init\', \'create_fb_entry\');
    
    function create_fb_entry_issue() { /* Create the \'Issue\' taxonomy */
        $labels = array(
            \'name\' => _x( \'Issue\', \'issue\' ),
            \'singular_name\' => _x( \'Issue\', \'issue\' ),
            \'search_items\' =>  __( \'Search Issues\' ),
            \'all_items\' => __( \'All Issues\' ),
            \'parent_item\' => __( \'Parent Issue\' ),
            \'parent_item_colon\' => __( \'Parent Issue:\' ),
            \'edit_item\' => __( \'Edit Issue\' ),
            \'update_item\' => __( \'Update Issue\' ),
            \'add_new_item\' => __( \'Add New Issue\' ),
            \'new_item_name\' => __( \'New Issue\' )
        );  
    
        register_taxonomy( \'issue\',
            array( \'fb_entry\' ),
                array(
                \'hierarchical\' => true,
                \'labels\' => $labels,
                \'show_ui\' => true,
                \'query_var\' => true,
                \'rewrite\' => array( 
                    \'slug\' => \'issue\',
                    \'with_front\'  => false
                )
            )
        );
    }
    add_action( \'init\', \'create_fb_entry_issue\', 0 );
    

    EDIT 2

    为了进一步说明,下面是我用来重写%issue%的代码:

    function fb_issue_permalink($permalink, $post_id, $leavename) {//   Fix dynamic \'issue\'/\'fb_entry\' slug in \'fb_entry\' permalink
        if (strpos($permalink, \'%issue%\') === FALSE) return $permalink;
    
            // Get post
            $post = get_post( $post_id );
            if( ! $post ) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms( $post->ID, \'issue\' );
            if( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) ) $taxonomy_slug = $terms[0]->slug;
    
            else $taxonomy_slug = \'unpublished\'; // Substitute \'journal issue\' in slug if \'journal entry\' is not categorized
    
            return str_replace(\'%issue%\', $taxonomy_slug, $permalink);
    }
    add_filter(\'post_link\', \'fb_issue_permalink\', 10, 3);
    add_filter(\'post_type_link\',\'fb_issue_permalink\', 10, 3);
    

  • 1 个回复
    最合适的回答,由SO网友:bode well carapace 整理而成

    我能够修复这个问题,并使用这个类指定重写规则的顺序:http://hmn.md/says/2012/11/08/wordpress-rewrite-rules-hm-core-style/

    结束

    相关推荐