Get Custom Taxonomies parents.
public static get_taxonomy_parents ( integer | WP_Term | object $term, string $taxonomy = 'category', boolean $link = false, string $separator = '/', boolean $nicename = false, array $visited = [] ) : string | ||
$term | integer | WP_Term | object | |
$taxonomy | string | |
$link | boolean | |
$separator | string | |
$nicename | boolean | |
$visited | array | |
return | string |
/** * * Get Custom Taxonomies parents. * @version 1.0 * */ public static function get_taxonomy_parents($id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array()) { $chain = ''; $parent = get_term($id, $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($parent->parent, $taxonomy, $link, $separator, $nicename, $visited); } if ($link) { $chain .= '<a href="' . get_term_link($parent->term_id, $taxonomy) . '" title="' . esc_attr(sprintf(__('View all posts in %s'), $parent->name)) . '">' . esc_html($name) . '</a>' . esc_html($separator); } else { $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 (get_option('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]; } $post_type_obj = get_post_type_object($post_type); $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; $front = substr($wp_rewrite->front, 1); $termlink = str_replace($front, '', $termlink); if ($with_front) { $slug = $front . $slug; } $termlink = str_replace($wp_home, $wp_home . '/' . $slug, $termlink); if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents($term->term_id, $taxonomy->name, false, '/', true), $termlink); } return $termlink; }
/** * * Fix taxonomy link outputs. * @since 0.6 * @version 1.0 * */ public function term_link($termlink, $term, $taxonomy) { if (get_option('no_taxonomy_structure')) { return $termlink; } $taxonomy = get_taxonomy($taxonomy); if ($taxonomy->_builtin) { return $termlink; } if (empty($taxonomy)) { return $termlink; } $wp_home = rtrim(home_url(), '/'); $post_type = $taxonomy->object_type[0]; $post_type_obj = get_post_type_object($post_type); $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; //拡張子を削除。 $str = array_shift(explode(".", get_option("permalink_structure"))); $str = rtrim(preg_replace("/%[a-z_]*%/", "", $str), '/'); //remove with front $termlink = str_replace($str . "/", "/", $termlink); if ($with_front === false) { $str = ""; } $slug = $str . "/" . $slug; $termlink = str_replace($wp_home, $wp_home . $slug, $termlink); if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents($term->term_id, $taxonomy->name, false, '/', true), $termlink); } return $termlink; }