自定义固定链接结构:/%自定义分类%/%POST-NAME%/(不带CPT插件)

时间:2019-02-10 作者:maksymdavydchuk

我正在尝试创建一个定制的permalink结构,它将允许我完成以下任务。

我有一个名为“products”的自定义帖子类型,我有一个名为“catalogs”的自定义分类法,分配给CPT“products”,我希望我的permalink结构如下所示:

目录/产品名称-无CPT slug或

%自定义分类法%/%帖子名称%/

还需要工作归档页(/产品)。

到目前为止,我已经成功实现了这样一种结构(/%post name%):

function remove_cpt_slug( $post_link, $post ) {
    if ( \'products\' === $post->post_type && \'publish\' === $post->post_status ) {
        $post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );
    }

    return $post_link;
}
add_filter( \'post_type_link\', \'remove_cpt_slug\', 10, 2 );

function parse_request ( $query ) {
    if ( ! $query->is_main_query() )
        return;

    if ( 2 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
        return;
    }

    if ( ! empty( $query->query[\'name\'] ) ) {
        $query->set( \'post_type\', array( \'post\', \'page\', \'products\' ) );
    }
}
add_action( \'pre_get_posts\', \'parse_request\' );
谢谢

1 个回复
SO网友:maksymdavydchuk

我设法找到了解决办法

$args   = array(
            \'labels\'             => $labels,
            \'public\'             => true,
            \'publicly_queryable\' => true,
            \'show_ui\'            => true,
            \'show_in_menu\'       => true,
            \'query_var\'          => true,
            \'rewrite\'            => array ( \'slug\' => \'products/%catalogs%\', \'with_front\' => false),
            \'capability_type\'    => \'post\',
            \'has_archive\'        => \'products\',
            \'hierarchical\'       => false,
            \'menu_position\'      => 4,
            \'menu_icon\'          => \'dashicons-cart\',
            \'supports\'           => array( \'title\', \'thumbnail\' ),
            \'taxonomies\'         => array( \'catalogs\' ),
        );
    register_post_type( \'products\', $args );

    register_taxonomy( \'catalogs\', \'products\', array(
            \'hierarchical\'      => true,
            \'labels\'            => $labels,
            \'show_ui\'           => true,
            \'query_var\'         => true,
            \'show_admin_column\' => true,
            \'show_tagcloud\'     => false,
            \'rewrite\'           => array( \'slug\' => \'products\', \'with_front\' => false ),
        ) );

    /** 
Remove taxony slug, remove CPT slug and add new rewrite rule 
*/
function remove_tax_slug_link( $link, $term, $taxonomy ) {
    if ( $taxonomy !== \'catalogs\' )
        return $link;

    return str_replace( \'products/\', \'\', $link );
}
add_filter( \'term_link\', \'remove_tax_slug_link\', 10, 3 );

function custom_tax_rewrite_rule() {
    $cats = get_terms(
            \'catalogs\', array(
            \'hide_empty\' => false,
        )
    );
    if( sizeof( $cats ) )
        foreach($cats as $cat)
            add_rewrite_rule( $cat->slug.\'/?$\', \'index.php?catalogs=\'.$cat->slug, \'top\' );
}
add_action(\'init\', \'custom_tax_rewrite_rule\');

function change_permalinks( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type == \'products\' ){
        $post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );
        $terms = wp_get_object_terms( $post->ID, \'catalogs\' );
        if( $terms ) {
            return str_replace( \'%catalogs%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( \'post_type_link\', \'change_permalinks\', 10, 2 );

function entry_rewrite_rules() {
    $custom_structure = \'/%catalogs%/%products%\';
    add_rewrite_tag( \'%products%\', \'([^/]+)\', \'products=\' );
    add_permastruct( \'products\', $custom_structure, array( \'walk_dirs\' => false ) );

}
add_action( \'init\', \'entry_rewrite_rules\' );

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。