There are two ways I’ve used for doing this…each with issues:
This one this only works if you’ve set the rewrite slug for your CPT. if you don’t set one it could lead to an error:
$post_type = get_queried_object();
echo $post_type->rewrite['slug'];
This one will not work on archive pages, because the page type will be “page”:
$post_type = get_post_type();
if ( $post_type )
{
$post_type_data = get_post_type_object( $post_type );
$post_type_slug = $post_type_data->rewrite['slug'];
echo $post_type_slug;
}
Related Posts:
- Remove slug from custom post type post URLs
- Redeclare/Change Slug of a Plugin’s Custom Post Type
- Custom Post Type Slug / Page Slug Conflict – Prevent use of reserved slug on page save?
- How to get custom post type label and singular label from its slug?
- Custom Post type & Taxonomy URL structure
- Slug Formatting : Acceptable Characters?
- Combining Multiple Taxonomies in one URL
- Using Custom Fields in Custom Post Type URL
- Change slug of registered custom post type in child theme
- How to achieve this permalink -> category-name/custom-post-type-name/post-name
- Prepend meta_value to permalink of post
- Custom Post Type – Same Post Name, Different Post Type
- Priority for Categories, Tags, Taxonomies, Posts, Pages and Custom Posts with same url
- Reslug a Custom Post Type
- Removing CPT slug from URL results in 404 error for archive page
- Force unique page slugs across all post types
- Getting Post ID from slug during init
- Custom Slug for a custom post type and default post
- Remove taxonomy slug when not assigning taxonomy with custom post types
- Custom post type’s slug gets wrong when adding a custom meta box
- Interesting Custom Post Type Slug with Taxonomy and Custom Field
- Get slug to point to different content of the same post
- Remove Taxonomy Slug when No Taxonomy is Assigned to Custom Post Type?
- Custom Permalink with Dynamic Taxonomy for Custom Post Type – Works, but breaks other permalinks
- Using custom post type parent as slug
- Custom post type with slug for plural (archive) and for single
- How do I fix permalinks for custom post types that has the same slug as a page?
- How to remove slugs from multiple custom post types
- Why does wordpress keep adding postname to my CPT’s permalink in the end?
- Wrong generated page_name/slug on first publish of custom post type
- Stop / prevent WordPress from updating permalinks of custom post type
- Rewrite slug for CPT
- Is there a way to have custom post type and page with the same slug?
- Using Same Slug With Multiple Post Types
- Adding Post id to the end of slug in a Custom Post Type
- Remove the parent slug in a CPT URL
- Permalink for a custom post type isn’t working and I don’t know why
- How to target a specific custom post type post and its all children and grandchildren?
- Index page with the same id as the slug
- Custom Post Type with Same Page URL
- Multiple (two) category postings on the same page.
- Removed custom post type slug but not working for child pages
- Including taxonomy term before post type breaks top level pages
- Can slugs from posts and custom post types conflict?
- Auto Slug Generation for Custom Post type
- Custom Post Type archive-{post-type}.php not working
- Custom post type single page (single-{CPT-name}) 404 when slug includes hyphens
- How do you create %post_type%/%postname%/ permalink structure?
- Slug for custom post type
- Slug Confusion Between Post and Custom Post types?
- Different slug taxonomy for two different CPT
- Change Slug of a Custom Post Type
- How can Custom Post Type and a page have the same slug?
- How to get postname in custom page?
- Changing slug of all posts
- WordPress Custom Post Type – Rewrite Query
- WordPress different templates for same type custom post (but different slug)
- Custom Post Type Advanced Slug
- Apply template by path/slug related to custom post type?
- Changing CPT slug and taxonomy already registered in parent theme
- Custom Post Type to replace Woocommerce products – page not found error
- Custom Post Type for Shortcode Use – Prevent it display as a post (with slug)
- Custom Post Types and Removing Slugs – should we do it?
- Need to return a string as permalink insted of str_replace
- Under What Conditions Can I name my CPT the same as my CPT Page?
- Custom post type and multi taxonomies + customize url
- Custom rewrite rule based on other custom post type title
- Use custom post type taxonomy between cpt slug and name
- Show code dependant on CPT & category
- Remove custom taxonamy slug with pagination
- How can I write slugs / permalinks as: custom post type -> custom taxonomy and custom post type -> custom taxonomy (one CPT and many taxo)?
- Add %post_id% to slug of custom post type and prevent the “unique slug” thing that WP does?
- Output all terms slugs for a loop filter
- Change permalink incrementor into pseudo-subdirectory
- add .html to custom post type with WPML
- Custom Post Type duplicating slug for new posts
- WordPress page not showing up – replaced with last 10 posts?
- Custom permalink rewrite rules – how do I fix this?
- WP gives 404 error for custom post type with GET variable
- Display a Custom Post Type within another with hierarchically slug
- Getting a 404 on single custom post type page when using rewrite on a custom taxonomy
- Generate slug and meta data if meta field is empty
- Costum post type with costum (sub)taxonomy
- Remove Custom Taxonomy Slug only without removing Custom post type slug in permalinks
- Can’t make permalink of CPT work
- Custom Post Type Rewrite Rule
- How can I retrieve the slug for a custom post type via name?
- WP Query WordPress Post Type with the same Taxonomy name
- Ideal top-level slug methods
- Create page using same slug as custom post type
- Hide custom post type slug url from search engine [closed]
- Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working
- Custom post type Premalinks main page and details page
- Removing custom post type name from permalink
- How to change permalink structure off default posts and also CPT’s posts
- Custom Post Type slug has the same Redirection entry
- How to change rewrite slug for custom post type without conflicting w/ child pages of page with same slug
- Custom post type URl Cutomization
- Right Permalink for Custom Post Type with number slug
- How to get taxonomy category in permalink for each taxonomy OR How to give a parent page to a given taxonomy?