自定义帖子类型网址重写

时间:2017-03-24 09:16:12

标签: wordpress url-rewriting

我正在将CPT'娱乐'的网址重写为/娱乐/%年%/%monthnum%/%post_id%/正在重写但却发现404未找到错误。我该如何解决这个问题?

这是我的代码:

add_action( 'init', 'my_rewrite');
function my_rewrite() {
global $wp_rewrite;
    $entertainment_structure = '/entertainment/%year%/%monthnum%/%post_id%/';
    $wp_rewrite->add_rewrite_tag("%entertainment%", '([^/]+)', "entertainment=");
    $wp_rewrite->add_permastruct('entertainment', $entertainment_structure, false);
    flush_rewrite_rules();
}

// Add filter to plugin init function
add_filter('post_type_link', 'entertainment_permalink', 10, 3);   
// Adapted from get_permalink function in wp-includes/link-template.php
function entertainment_permalink($permalink, $post_id, $leavename) {
    $post = get_post($post_id);
    $rewritecode = array(
        '%year%',
        '%monthnum%',
        '%day%',
        '%hour%',
        '%minute%',
        '%second%',
        $leavename? '' : '%post_id%',
        '%post_id%',
        '%category%',
        '%author%',
        $leavename? '' : '%post_id%',
    );

    if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
        $unixtime = strtotime($post->post_date);

        $category = '';
        if ( strpos($permalink, '%category%') !== false ) {
            $cats = get_the_category($post->ID);
            if ( $cats ) {
                usort($cats, '_usort_terms_by_ID'); // order by ID
                $category = $cats[0]->slug;
                if ( $parent = $cats[0]->parent )
                    $category = get_category_parents($parent, false, '/', true) . $category;
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if ( empty($category) ) {
                $default_category = get_category( get_option( 'default_category' ) );
                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
            }
        }

        $author = '';
        if ( strpos($permalink, '%author%') !== false ) {
            $authordata = get_userdata($post->post_author);
            $author = $authordata->user_nicename;
        }

        $date = explode(" ",date('Y m d H i s', $unixtime));
        $rewritereplace =
        array(
            $date[0],
            $date[1],
            $date[2],
            $date[3],
            $date[4],
            $date[5],
            $post->ID,
            $post->ID,
            $category,
            $author,
            $post->ID,
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } else { // if they're not using the fancy permalink option
    }
    return $permalink;
}

如果我更换$ entertainment_structure ='/ entertainment /%year%/%monthnum%/%post_id%/';与$ entertainment_structure ='/娱乐/%年%/%monthnum%/%娱乐%/';它工作正常

1 个答案:

答案 0 :(得分:0)

你不应该挂钩init,但是如果它是一个插件,则注册register_activation_hook(),或者如果它是一个主题,则使用after_switch_theme(重要的是停用/重新激活插件或切换主题以便重写申请)。 < =进一步阅读here

您可能希望阅读the rewrite API