custom post types - CPT and rewrite rules

admin2025-04-18  0

I have CPT "Library" with "Library-category" taxonomy.

What I want to achieve is to have:

  • - archive page for Library CPT
  • / - archive for Library-category taxonomy term "Case study"
  • - single post of Library CPT within "Case study taxonomy".

I've got it mostly to work with:

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,
        ),
    )
);

However, the doesn't work and returns 404. How can I fix it?

I have CPT "Library" with "Library-category" taxonomy.

What I want to achieve is to have:

  • https://example/library - archive page for Library CPT
  • https://example/library/case-study/ - archive for Library-category taxonomy term "Case study"
  • https://example/library/case-study/how-we-won - single post of Library CPT within "Case study taxonomy".

I've got it mostly to work with:

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,
        ),
    )
);

However, the https://example/library doesn't work and returns 404. How can I fix it?

Share Improve this question asked Dec 1, 2019 at 11:58 Karolína VyskočilováKarolína Vyskočilová 4291 gold badge8 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Finally, found a solution mix:

The CPT has to have:

 'has_archive'         => 'library', // Fixes the archive URL.
 'rewrite'             => array(
    'slug'       => 'library/%library-category%',
    'with_front' => false,
 )

Taxonomy should be as following:

'rewrite'        => array(
    'slug'       => 'library',
    'with_front' => false,
),

And than you need to rewrite the links:

/**
 * 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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744951190a276346.html

最新回复(0)