TransWikia.com

How Can I Change the Custom Post Type Slug to the Taxonomy Slug in the URL?

WordPress Development Asked on November 14, 2021

(Forgive me, I know this question has been asked extensively but I haven’t found an answer that works for me)

I’m working on a website for a webcomic. I have a custom post type set up called "comic-page" (for individual comic pages) and a taxonomy called "comic-series" (as I plan for the comic to span across multiple series). Right now the URL for a typical comic page looks like:

http://localhost/wordpress/comic-page/page-01/

When I want it to look like:

http://localhost/wordpress/lcc/page-01/ 

(Where "lcc" is the slug of the particular series)

I’ve tried several methods which have successfully removed "comic-page" from the URL but none which introduce "lcc" into the URL. My current working code is as follows (I’ve taken out the bits which remove "comic-page"):

Comic Page Post Type:

/* === Comic Page === */
function post_type_comic_page() {
    
    // Labels
    $labels = array(
        'name' => 'Comic Pages',
        'singular_name' => 'Comic Page',
        'add_new' => 'Add New Comic Page',
        'add_new_item' => 'Add Comic Page',
        'all_items' => 'All Comic Pages',
        'edit_item' => 'Edit Comic Page',
        'not_found' => 'No Comic Pages Found',
        'not_found_in_trash' => 'No Comic Pages Found in Trash',
    );
    
    // Arguments
    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_icon' => 'dashicons-book-alt',
        'hierarchical' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'publicly_queryable' => true,
        'supports' => array( 'title', 'page-attributes' ),
    );
    
    // Register Comic Pages Post Type
    register_post_type('comic-page', $args);
}

add_action('init', 'post_type_comic_page');

Series Taxonomy:

/* === Comic Series Taxonomy === */
function taxonomy_comic_series() {
    
    // Labels
    $labels = array(
        'name' => 'Series',
        'singular_name' => 'Series',
        'add_new' => 'Add New',
        'add_new_item' => 'Add Series',
        'all_items' => 'All Series',
        'edit_item' => 'Edit Series',
        'not_found' => 'No Series Found',
        'not_found_in_trash' => 'No Series Found in Trash',
    );
    
    // Arguments
    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'comic-series', 'with_front' => false ),
    );
    
    // Register Comic Page Type Taxonomy
    register_taxonomy('comic-series', 'comic-page', $args);
}

add_action('init', 'taxonomy_comic_series');

My endgoal is to be able to select which series a particular comic page belongs to on the page itself and then have the corresponding slug which I’ve assigned on the WordPress backend appear in the URL. I’m sure there’s a way to achieve what you’d think would be a relatively straightforward thing to do, but I can’t figure it out. Can anyone help?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP