首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CPT和重写规则

CPT和重写规则
EN

WordPress Development用户
提问于 2019-12-01 11:58:39
回答 1查看 23关注 0票数 0

我有CPT“图书馆”和“图书馆-类别”分类法。

我想达到的目标是:

我有很多事情要做:

代码语言:javascript
复制
register_post_type(
    'library',
    array(
        'label'               => __( 'Library', 'my-theme' ),
        'description'         => __( 'Library', 'my-theme' ),
        'labels'              => array(
            'name'           => _x( 'Library', 'Post Type General Name', 'my-theme' ),
            'singular_name'  => _x( 'Library item', 'Post Type Singular Name', 'my-theme' ),
            'menu_name'      => __( 'Library', 'my-theme' ),
            'name_admin_bar' => __( 'Library item', 'my-theme' ),
            'add_new_item'   => __( 'Create New Library item', 'my-theme' ),
            'add_new'        => __( 'Add Library item', 'my-theme' ),
        ),
        'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'thumbnail' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 11,
        'menu_icon'           => 'dashicons-analytics',
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => true,
        'can_export'          => true,
        'has_archive'         => true,
        'rewrite'             => array(
            'slug'       => 'library/%library-category%',
            'with_front' => false,
        ),
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'show_in_rest'        => true,
    )
);

register_taxonomy(
    'library-category',
    array( 'library' ),
    array(
        'labels'            => array(
            'name'          => _x( 'Categories', 'Taxonomy General Name', 'my-theme' ),
            'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'my-theme' ),
        ),
        'hierarchical'      => true,
        'public'            => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'show_tagcloud'     => false,
        'show_in_rest'      => true,
        'rewrite'             => array(
            'slug'       => 'library/%library-category%/',
            'with_front' => false,
        ),
    )
);

但是,https://example.com/library不工作,并返回404。我怎么才能修好它?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-12-01 12:06:34

最后,找到了一个解决方案组合:

欧洲禁止酷刑委员会必须有:

代码语言:javascript
复制
 'has_archive'         => 'library', // Fixes the archive URL.
 'rewrite'             => array(
    'slug'       => 'library/%library-category%',
    'with_front' => false,
 )

分类应如下:

代码语言:javascript
复制
'rewrite'        => array(
    'slug'       => 'library',
    'with_front' => false,
),

而你需要重写链接:

代码语言:javascript
复制
/**
 * Custom rewrite rules for URL
 *
 * @param string  $post_link Post link url.
 * @param integer $id Post ID.
 * @return string
 */
function getmanta_technologies_post_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) ) {     
        $terms_lc = wp_get_object_terms( $post->ID, 'library-category' );
        if ( $terms_lc ) {
            return str_replace( '%library-category%', $terms_lc[0]->slug, $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'getmanta_technologies_post_link', 1, 3 );
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/353692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档