Instantly share code, notes, and snippets.
Last active
March 16, 2023 08:10
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save karamansky/108dba467be51c6d26e373d1c7f46b3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function prepareSubtitleItemsForAutomaticBreadcrumbs() { | |
global $post; | |
//default first breadcrumb | |
if( !is_front_page() ) { | |
$subtitle_items[] = [ | |
'url' => get_home_url(), | |
'title' => __( 'Home', 'hayes' ) | |
]; | |
} | |
if (is_single()) { | |
if( is_singular() ){ | |
$post_type = get_post_type_object(get_post_type()); | |
$post_type_archive = get_post_type_archive_link($post_type->name); | |
if( $post_type->name == 'team' ){ | |
$post_type_archive = '/team'; | |
} | |
$subtitle_items[] = [ | |
'url' => $post_type_archive, | |
'title' => $post_type->labels->name | |
]; | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_title() | |
]; | |
}else{ | |
$categories = get_the_category(); | |
$category = $categories[0]; | |
$cat_id = $category->cat_ID; | |
if ($cat_id > 0) { | |
$subtitle_items[] = [ | |
'url' => get_category_link($cat_id), | |
'title' => $category->cat_name | |
]; | |
} | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_title() | |
]; | |
} | |
} elseif ( is_page() ) { | |
$ancestors = get_post_ancestors($post); | |
if (!empty($ancestors)) { | |
$ancestors = array_reverse($ancestors); | |
foreach ($ancestors as $ancestor) { | |
$subtitle_items[] = [ | |
'url' => get_permalink($ancestor), | |
'title' => get_the_title($ancestor) | |
]; | |
} | |
} | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_title() | |
]; | |
} elseif ( is_category() ) { | |
$category = get_category(get_query_var('cat')); | |
if ($category->parent != 0) { | |
$parent_category = get_category($category->parent); | |
$subtitle_items[] = [ | |
'url' => get_category_link($parent_category->cat_ID), | |
'title' => $parent_category->cat_name | |
]; | |
} | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => single_cat_title('', false) | |
]; | |
} elseif ( is_tag() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => single_tag_title('', false) | |
]; | |
} elseif ( is_author() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_author_meta('display_name', get_query_var('author')) | |
]; | |
} elseif ( is_archive() ) { | |
if (is_day()) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_date() | |
]; | |
} elseif ( is_month() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_date('F Y') | |
]; | |
} elseif ( is_year() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_date('Y') | |
]; | |
} elseif ( is_category() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => single_cat_title('', false) | |
]; | |
} elseif ( is_tag() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => single_tag_title('', false) | |
]; | |
} elseif ( is_author() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => get_the_author_meta('display_name', get_query_var('author')) | |
]; | |
} else { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => post_type_archive_title('', false) | |
]; | |
} | |
} elseif ( is_search() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => 'Search' | |
]; | |
} elseif ( is_404() ) { | |
$subtitle_items[] = [ | |
'url' => '', | |
'title' => '404' | |
]; | |
} | |
return $subtitle_items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment