通过自定义域和帖子将自定义值添加到固定链接

时间:2014-05-14 作者:asaunders

我有两种自定义帖子类型,一种叫做瓜党,另一种叫做党类别。对于甜瓜派对中的帖子,我创建了一个元框,该元框从派对类别中的所有帖子中填充。然后,我可以将甜瓜parites的url重写到www.domain。com/派对/圣诞派对/泰晤士河派对

其中圣诞派对是来自派对类别的自定义帖子。

我可以访问该页面(www.domain.com/parties/christmas party/thames river party),但当我尝试访问www.domain时。com/parties/christmas parties它重定向到主页,而不是自定义帖子,这就是我的问题所在。我也不得不为此做一个自定义重写,因为当我注册帖子类型时使用重写选项时,我在派对帖子上得到了404。

下面是我创建所有自定义帖子类型和重写规则的代码:

function register_custom_post_types() {
    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Parties\' ),
            \'singular_name\' => __( \'Party\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'has_archive\' => \'true\',
        \'hierarchical\' => true,
        \'taxonomies\' => array(\'post_tag\'),
        \'capability_type\' => \'post\',
        \'rewrite\' => false,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-parties\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Type of Parties\' ),
            \'singular_name\' => __( \'Type of Party\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'has_archive\' => \'true\',
        \'hierarchical\' => true,
        \'show_ui\' => true,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'page\',
        \'supports\' => array(\'title\', \'thumbnail\', \'revisions\', \'page-attributes\'),
        \'rewrite\' => false
    );

    register_post_type(\'party-categories\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Home Slider\' ),
            \'singular_name\' => __( \'Home Slide\' )
        ),
        \'menu_position\' => 50,
        \'public\' => false,
        \'show_ui\' => true,
        \'hierarchical\' => false,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-home-slider\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Case Studies\' ),
            \'singular_name\' => __( \'Case Study\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'hierarchical\' => false,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'rewrite\' => array(\'with_front\' => false, \'slug\' => \'case-studies\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-case-studies\', $args);
}
add_action( \'init\', \'register_custom_post_types\' );

add_action(\'init\', \'tdd_add_rewrite_rules\');

function tdd_add_rewrite_rules() {

// Register custom rewrite rules

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag(\'%melon-parties%\', \'([^/]+)\', \'melon-parties=\');
    $wp_rewrite->add_rewrite_tag(\'%parties_categories%\', \'([^/]+)\', \'cmb-event_category=\');

    $wp_rewrite->add_permastruct(\'melon-parties\', \'/parties/%parties_categories%/%melon-parties%\', false);
}

add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);

    function tdd_permalinks($permalink, $post, $leavename) {

    $no_data = \'no-category\';

    $post_id = $post->ID;

    if($post->post_type != \'melon-parties\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))

    return $permalink;

    $var1 = get_post_meta($post_id, \'cmb-event_category\', true);

    $var1 = sanitize_title($var1);

    if(!$var1) { $var1 = $no_data; }

    $permalink = str_replace(\'%parties_categories%\', $var1, $permalink);

    return $permalink;

}

add_action(\'init\', \'partyCategoryRewriteRules\');

function partyCategoryRewriteRules() {

// Register custom rewrite rules

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag(\'%party-categories%\', \'([^/]+)\', \'party-categories=\');

    $wp_rewrite->add_permastruct(\'party-categories\', \'/parties/%category%\', false);
}

add_filter(\'post_type_link\', \'partyCategoryPermalinks\', 10, 3);

    function partyCategoryPermalinks($permalink, $post, $leavename) {

    $no_data = \'no-category\';

    $post_id = $post->ID;

    if($post->post_type != \'party-categories\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))

    return $permalink;

    $var1 = $post->post_name;

    $var1 = sanitize_title($var1);

    if(!$var1) { $var1 = $no_data; }

    $permalink = str_replace(\'%category%\', $var1, $permalink);

    return $permalink;

}
请有人告诉我哪里出了问题。非常感谢。

1 个回复
最合适的回答,由SO网友:asaunders 整理而成

我通过在函数中添加重写规则修复了此问题。顶部的php文件:

function melon_rewrites($rules) {
        $new_rules = array(
            \'parties/([^/]*)/([^/]*)$\' => \'index.php?melon-parties=$matches[2]\'
        );
        $rules = $new_rules + $rules;
        return $rules;
    }
    add_filter(\'rewrite_rules_array\', \'melon_rewrites\');
然后,我更改了cpt以允许查询变量:

function register_custom_post_types() {
    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Parties\' ),
            \'singular_name\' => __( \'Party\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'has_archive\' => \'true\',
        \'hierarchical\' => true,
        \'taxonomies\' => array(\'party-tags\'),
        \'capability_type\' => \'post\',
        \'query_var\' => true,
        \'rewrite\' => false,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-parties\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Type of Parties\' ),
            \'singular_name\' => __( \'Type of Party\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'has_archive\' => \'true\',
        \'hierarchical\' => true,
        \'show_ui\' => true,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'page\',
        \'query_var\' => true,
        \'supports\' => array(\'title\', \'thumbnail\', \'revisions\', \'page-attributes\'),
        \'rewrite\' => false
    );

    register_post_type(\'party-categories\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Home Slider\' ),
            \'singular_name\' => __( \'Home Slide\' )
        ),
        \'menu_position\' => 50,
        \'public\' => false,
        \'show_ui\' => true,
        \'hierarchical\' => false,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-home-slider\', $args);

    $args = array(
        \'labels\' => array(
            \'name\' => __( \'Case Studies\' ),
            \'singular_name\' => __( \'Case Study\' )
        ),
        \'menu_position\' => 50,
        \'public\' => true,
        \'hierarchical\' => false,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'rewrite\' => array(\'with_front\' => false, \'slug\' => \'case-studies\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );

    register_post_type(\'melon-case-studies\', $args);
}
add_action( \'init\', \'register_custom_post_types\' );
我还必须对我的重写规则做一些更改,因为我实际上并没有指向我的cpt:

add_action(\'init\', \'tdd_add_rewrite_rules\');

    function tdd_add_rewrite_rules() {

    // Register custom rewrite rules
        add_rewrite_tag(\'%melon-parties%\', \'([^/]+)\');
        add_rewrite_tag(\'%party-categories%\', \'([^/]+)\');

        add_permastruct(\'melon-parties\', \'parties/%party-categories%/%melon-parties%\', false);
    }

    add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);

        function tdd_permalinks($permalink, $post, $leavename) {

        $no_data = \'no-category\';

        $post_id = $post->ID;

        if($post->post_type != \'melon-parties\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))

        return $permalink;

        $var1 = get_post_meta($post_id, \'cmb-event_category\', true);

        $var1 = get_the_title($var1);

        $var1 = sanitize_title($var1);

        if(!$var1) { $var1 = $no_data; }

        $permalink = str_replace(\'%party-categories%\', $var1, $permalink);

        return $permalink;

    }

    add_action(\'init\', \'partyCategoryRewriteRules\');

    function partyCategoryRewriteRules() {

    // Register custom rewrite rules

        global $wp_rewrite;
        $wp_rewrite->add_rewrite_tag(\'%party-categories%\', \'([^/]+)\', \'party-categories=\');

        $wp_rewrite->add_permastruct(\'party-categories\', \'/parties/%party-categories%\', false);
    }

    add_filter(\'post_type_link\', \'partyCategoryPermalinks\', 10, 3);

        function partyCategoryPermalinks($permalink, $post, $leavename) {

        $no_data = \'no-category\';

        $post_id = $post->ID;

        if($post->post_type != \'party-categories\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))

        return $permalink;

        $var1 = $post->post_name;

        $var1 = sanitize_title($var1);

        if(!$var1) { $var1 = $no_data; }

        $permalink = str_replace(\'%party-categories%\', $var1, $permalink);

        return $permalink;

    }
现在我的url结构正确了

结束

相关推荐