Skip to main content
added 403 characters in body
Source Link
Jacob Peattie
  • 44.4k
  • 10
  • 51
  • 65

You can access the post type properties using get_post_type_object(). The rewrite argument will be a property if the returned object:

$post_type_object = get_post_type_object( 'products' ); $rewrite_slug = $post_type_object->rewrite['slug']; 

Update

I needed this for permalinks inside a template part where there are categories displayed.

No you don't. To get the URL to a category archive, use get_term_link():

<a href="<?php echo esc_url( get_term_link( $category ) ); ?>" class="cat-bar__label"><?php echo $category->name; ?></a> 

You can access the post type properties using get_post_type_object(). The rewrite argument will be a property if the returned object:

$post_type_object = get_post_type_object( 'products' ); $rewrite_slug = $post_type_object->rewrite['slug']; 

You can access the post type properties using get_post_type_object(). The rewrite argument will be a property if the returned object:

$post_type_object = get_post_type_object( 'products' ); $rewrite_slug = $post_type_object->rewrite['slug']; 

Update

I needed this for permalinks inside a template part where there are categories displayed.

No you don't. To get the URL to a category archive, use get_term_link():

<a href="<?php echo esc_url( get_term_link( $category ) ); ?>" class="cat-bar__label"><?php echo $category->name; ?></a> 
Source Link
Jacob Peattie
  • 44.4k
  • 10
  • 51
  • 65

You can access the post type properties using get_post_type_object(). The rewrite argument will be a property if the returned object:

$post_type_object = get_post_type_object( 'products' ); $rewrite_slug = $post_type_object->rewrite['slug'];