How do I grab the parent category and subcategory, i.e.
aaa-1ndustries/beauty
?
Well if you meant the current category URL, where it looks like https://example.com/blog/topic/aaa-1ndustries/beauty/
, and you just wanted to get the above category path from that URL, then try this:
$request = $GLOBALS['wp']->request;
$page_path = trim( preg_replace( '#^blog/topic/#', '', $request ), "https://wordpress.stackexchange.com/" );
And if you want to do the same for a specific category URL/permalink, you can use get_category_link()
(or get_term_link()
for custom taxonomies) and parse_url()
:
$cat_id = 123; // change the value to the correct category ID
$request = parse_url( get_category_link( $cat_id ), PHP_URL_PATH );
$page_path = trim( preg_replace( '#^/blog/topic/#', '', $request ), "https://wordpress.stackexchange.com/" );
But if you actually wanted to get the actual hierarchical path of the category, then use get_category_parents()
(or get_term_parents_list()
for custom taxonomies). E.g.
// Use this for the core `category` taxonomy:
$cat_id = get_queried_object_id(); // or just set a specific category ID
$cat_path = get_category_parents( $cat_id, false, "https://wordpress.stackexchange.com/", true );
// OR use this for custom taxonomies:
$term = get_queried_object(); // or just set a specific term object
$cat_path = get_term_parents_list( $term->term_id, $term->taxonomy, array(
'separator' => "https://wordpress.stackexchange.com/",
'link' => false,
'format' => 'slug',
) );
echo trim( $cat_path, "https://wordpress.stackexchange.com/" ); // the trim() removes the trailing/last slash