Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress后期重写规则导致页面404_Php_Wordpress_Permalinks - Fatal编程技术网

Php WordPress后期重写规则导致页面404

Php WordPress后期重写规则导致页面404,php,wordpress,permalinks,Php,Wordpress,Permalinks,我需要我的post permalinks包含一个自定义分类值,type,如下所示: http://staging.mysite.com/article/post-title 其中,article是该帖子的类型分类值。我已经做到了这一点,但我遇到的问题是,现在我的网站的所有页面都是404。我的自定义帖子类型和普通帖子URL按预期工作,只是页面URL被破坏了。以下是导致页面URL出现问题的代码: // Type taxonomy (no issues here) function create_ty

我需要我的post permalinks包含一个自定义分类值,
type
,如下所示:

http://staging.mysite.com/article/post-title

其中,
article
是该帖子的
类型
分类值。我已经做到了这一点,但我遇到的问题是,现在我的网站的所有页面都是404。我的自定义帖子类型和普通帖子URL按预期工作,只是页面URL被破坏了。以下是导致页面URL出现问题的代码:

// Type taxonomy (no issues here)
function create_type_taxonomy() {
    register_taxonomy(
        'type',
        'post',
        array(
            'labels' => array(
                'name' => 'Type',
                'add_new_item' => 'Add New Type',
                'new_item_name' => 'New Type'
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => false,
            'rewrite' => array(
                'slug' => 'type',
                'with_front' => true
            ),
        )
    );
}

add_action( 'init', 'create_type_taxonomy', 0 );

add_action( 'init', 'st_default_post_type', 1 );

// Re-register built-in posts with a custom rewrite rule
function st_default_post_type() {
    register_post_type( 'post', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
        ),
        'public'  => true,
        '_builtin' => false, 
        '_edit_link' => 'post.php?post=%d', 
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => '%type%', 'with_front' => false ), // custom rewrite rule
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) );
}

add_filter('post_type_link', 'st_posts_permalink_structure', 10, 4);

// Replace custom rewrite rule on posts (%type%) with the taxonomy value
function st_posts_permalink_structure($post_link, $post, $leavename, $sample){
    if ($post->post_type != 'post') {
        var_dump($post->post_type);
        return $post_link;
    }
    else{
        if (strpos($post_link, '%type%') === FALSE){
            return $post_link;
        }

        $post = get_post($post);
        if (!$post){ 
            return $post_link;
        }

        $terms = wp_get_object_terms($post->ID, 'type');

        if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) ){
            $taxonomy_slug = $terms[0]->slug;
        }
        else{
            $taxonomy_slug = 'type';
        }

        return str_replace('%type%', $taxonomy_slug, $post_link);
    }
}
希望另一组眼睛能捕捉到一些东西,导致页面永久链接到404。我已经尝试将管理员中的permalinks设置更改为
/%type%/%postname%/
,但这也有同样的问题。我在这里发现了几个其他问题,看起来与我的问题相同,但没有一个得到回答: