Get Custom Taxonomies parents slug.
public static get_taxonomy_parents_slug ( integer | WP_Term | object $term, string $taxonomy = 'category', string $separator = '/', boolean $nicename = false, array $visited = [] ) : string | ||
$term | integer | WP_Term | object | |
$taxonomy | string | |
$separator | string | |
$nicename | boolean | |
$visited | array | |
return | string |
/** * * Get Custom Taxonomies parents slug. * * @version 1.0 * * @param int|WP_Term|object $term * @param string $taxonomy * @param string $separator * @param bool $nicename * @param array $visited * * @return string */ public static function get_taxonomy_parents_slug($term, $taxonomy = 'category', $separator = '/', $nicename = false, $visited = array()) { $chain = ''; $parent = get_term($term, $taxonomy); if (is_wp_error($parent)) { return $parent; } if ($nicename) { $name = $parent->slug; } else { $name = $parent->name; } if ($parent->parent && $parent->parent != $parent->term_id && !in_array($parent->parent, $visited)) { $visited[] = $parent->parent; $chain .= CPTP_Util::get_taxonomy_parents_slug($parent->parent, $taxonomy, $separator, $nicename, $visited); } $chain .= $name . $separator; return $chain; }
/** * * Fix taxonomy link outputs. * * @since 0.6 * @version 1.0 * * @param string $termlink * @param Object $term * @param Object $taxonomy * * @return string */ public function term_link($termlink, $term, $taxonomy) { /** @var WP_Rewrite $wp_rewrite */ global $wp_rewrite; if (!$wp_rewrite->permalink_structure) { return $termlink; } if (CPTP_Util::get_no_taxonomy_structure()) { return $termlink; } $taxonomy = get_taxonomy($taxonomy); if ($taxonomy->_builtin) { return $termlink; } if (empty($taxonomy)) { return $termlink; } $wp_home = rtrim(home_url(), '/'); if (in_array(get_post_type(), $taxonomy->object_type)) { $post_type = get_post_type(); } else { $post_type = $taxonomy->object_type[0]; } $front = substr($wp_rewrite->front, 1); $termlink = str_replace($front, '', $termlink); // remove front. $post_type_obj = get_post_type_object($post_type); if (empty($post_type_obj)) { return $termlink; } $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; if ($with_front) { $slug = $front . $slug; } if (!empty($slug)) { $termlink = str_replace($wp_home, $wp_home . '/' . $slug, $termlink); } if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents_slug($term->term_id, $taxonomy->name, '/', true), $termlink); } return $termlink; }