| @@ -38,19 +38,9 @@ | ||
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | if (!function_exists('get_easyel_breadcrumb')) { |
| 42 | - function get_easyel_breadcrumb( | |
| 43 | - $custom_title = '', | |
| 44 | - $custom_path = '', | |
| 45 | - $custom_separator = '', | |
| 46 | - $custom_home_title = '', | |
| 47 | - $search_custom_title = '', | |
| 48 | - $search_result_title = '', | |
| 49 | - $show_category_path = true, | |
| 50 | - $home_icon_url = '', | |
| 51 | - $home_icon_picker = '' | |
| 52 | - ) { | |
| 42 | + function get_easyel_breadcrumb($custom_title='', $custom_path='', $custom_separator='', $custom_home_title='', $search_custom_title='', $search_result_title='', $show_category_path = true, $home_icon_url = '', $home_icon_picker = ''){ | |
| 53 | 43 | global $post; |
| 54 | 44 | $post = get_queried_object(); |
| 55 | 45 | $object_id = get_queried_object_id(); |
| 56 | 46 | $home_title = !empty($custom_home_title) ? $custom_home_title : 'Home'; |
| @@ -55,21 +45,26 @@ | ||
| 55 | 45 | $object_id = get_queried_object_id(); |
| 56 | 46 | $home_title = !empty($custom_home_title) ? $custom_home_title : 'Home'; |
| 57 | 47 | |
| 58 | 48 | // Separator |
| 59 | - $separator = !empty($custom_separator) ? $custom_separator : '<span class="breadcrumb-separator">/</span>'; | |
| 49 | + $separator = '<span class="breadcrumb-separator">/</span>'; | |
| 50 | + if (!empty($custom_separator)) { | |
| 51 | + $separator = $custom_separator; | |
| 52 | + } | |
| 60 | 53 | |
| 61 | 54 | // Home icon |
| 62 | - $home_icon = !empty($home_icon_picker) ? $home_icon_picker : ''; | |
| 55 | + $home_icon = ''; | |
| 56 | + if (!empty($home_icon_picker)) { | |
| 57 | + $home_icon = $home_icon_picker; | |
| 58 | + } | |
| 59 | + | |
| 63 | 60 | $home_link = '<a href="' . esc_url(home_url('/')) . '">' . $home_icon . (!empty($home_icon) ? ' ' : '') . '<span class="breadcrumb-home-text">' . esc_html($home_title) . '</span></a>'; |
| 64 | 61 | $output = $home_link; |
| 65 | 62 | |
| 66 | - // Single Post | |
| 67 | 63 | if (is_single()) { |
| 68 | 64 | $post_type = get_post_type(); |
| 69 | 65 | |
| 70 | 66 | if ($post_type != 'post') { |
| 71 | - // Custom Post Type | |
| 72 | 67 | $post_type_object = get_post_type_object($post_type); |
| 73 | 68 | if ($post_type_object && $post_type_object->has_archive) { |
| 74 | 69 | $output .= $separator . '<a href="' . esc_url(get_post_type_archive_link($post_type)) . '">' . esc_html($post_type_object->labels->name) . '</a>'; |
| 75 | 70 | } |
| @@ -94,39 +89,27 @@ | ||
| 94 | 89 | } |
| 95 | 90 | } |
| 96 | 91 | } |
| 97 | 92 | |
| 98 | - // Categories for normal posts | |
| 93 | + // Categories for normal post | |
| 99 | 94 | if ($post_type == 'post' && $show_category_path) { |
| 100 | - $categories = get_the_category($object_id); | |
| 101 | - if (!empty($categories) && !is_wp_error($categories)) { | |
| 102 | - $main_cat = $categories[0]; | |
| 103 | - | |
| 104 | - // Include parent categories | |
| 105 | - $parent_cats = get_ancestors($main_cat->term_id, 'category'); | |
| 106 | - if (!empty($parent_cats)) { | |
| 107 | - $parent_cats = array_reverse($parent_cats); | |
| 108 | - foreach ($parent_cats as $parent_id) { | |
| 109 | - $parent_term = get_term($parent_id, 'category'); | |
| 110 | - if ($parent_term && !is_wp_error($parent_term)) { | |
| 111 | - $output .= $separator . '<a href="' . esc_url(get_term_link($parent_term)) . '">' . esc_html($parent_term->name) . '</a>'; | |
| 112 | - } | |
| 113 | - } | |
| 114 | - } | |
| 115 | - | |
| 116 | - // Main category | |
| 117 | - $output .= $separator . '<a href="' . esc_url(get_term_link($main_cat)) . '">' . esc_html($main_cat->name) . '</a>'; | |
| 95 | + $categories = get_the_category(); | |
| 96 | + if ($categories) { | |
| 97 | + $category = $categories[0]; | |
| 98 | + $cat_parents = get_category_parents($category, true, $separator); | |
| 99 | + $cat_parents = preg_replace('/(?<!>)\b([^<]+)\b(?!<\/a>)/', '<span class="breadcrumb-text">$1</span>', $cat_parents); | |
| 100 | + $output .= $separator . $cat_parents; | |
| 118 | 101 | } |
| 119 | 102 | } |
| 120 | 103 | |
| 121 | - // Post title | |
| 104 | + // Post title (added only once) | |
| 122 | 105 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html(get_the_title()) . '</span>'; |
| 123 | 106 | } |
| 124 | 107 | |
| 125 | - // Page | |
| 126 | 108 | elseif (is_page()) { |
| 127 | 109 | if ($post->post_parent) { |
| 128 | - $ancestors = array_reverse(get_post_ancestors($post->ID)); | |
| 110 | + $ancestors = get_post_ancestors($post->ID); | |
| 111 | + $ancestors = array_reverse($ancestors); | |
| 129 | 112 | foreach ($ancestors as $ancestor) { |
| 130 | 113 | $output .= $separator . '<a href="' . esc_url(get_permalink($ancestor)) . '">' . esc_html(get_the_title($ancestor)) . '</a>'; |
| 131 | 114 | } |
| 132 | 115 | } |
| @@ -132,14 +115,14 @@ | ||
| 132 | 115 | } |
| 133 | 116 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html(get_the_title()) . '</span>'; |
| 134 | 117 | } |
| 135 | 118 | |
| 136 | - // Category / Tag / Taxonomy | |
| 137 | 119 | elseif (is_category() || is_tag() || is_tax()) { |
| 138 | 120 | $term = get_queried_object(); |
| 139 | 121 | if ($term && !is_wp_error($term)) { |
| 140 | 122 | if ($term->parent != 0) { |
| 141 | - $ancestors = array_reverse(get_ancestors($term->term_id, $term->taxonomy)); | |
| 123 | + $ancestors = get_ancestors($term->term_id, $term->taxonomy); | |
| 124 | + $ancestors = array_reverse($ancestors); | |
| 142 | 125 | foreach ($ancestors as $ancestor) { |
| 143 | 126 | $ancestor_term = get_term($ancestor, $term->taxonomy); |
| 144 | 127 | $output .= $separator . '<a href="' . esc_url(get_term_link($ancestor_term)) . '">' . esc_html($ancestor_term->name) . '</a>'; |
| 145 | 128 | } |
| @@ -147,32 +130,28 @@ | ||
| 147 | 130 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html(single_term_title('', false)) . '</span>'; |
| 148 | 131 | } |
| 149 | 132 | } |
| 150 | 133 | |
| 151 | - // Post type archive | |
| 152 | 134 | elseif (is_post_type_archive()) { |
| 153 | 135 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html(post_type_archive_title('', false)) . '</span>'; |
| 154 | 136 | } |
| 155 | 137 | |
| 156 | - // Blog page | |
| 157 | 138 | elseif (is_home() && !is_front_page()) { |
| 158 | - $blog_title = get_the_title(get_option('page_for_posts')); | |
| 139 | + $blog_title = get_the_title(get_option('page_for_posts', true)); | |
| 159 | 140 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html($blog_title) . '</span>'; |
| 160 | 141 | } |
| 161 | 142 | |
| 162 | - // Search | |
| 163 | 143 | elseif (is_search()) { |
| 164 | 144 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html__('Search Results for:', 'easy-elements') . ' ' . esc_html(get_search_query()) . '</span>'; |
| 165 | 145 | } |
| 166 | 146 | |
| 167 | - // 404 | |
| 168 | 147 | elseif (is_404()) { |
| 169 | 148 | $output .= $separator . '<span class="breadcrumb-text">' . esc_html__('404 Not Found', 'easy-elements') . '</span>'; |
| 170 | 149 | } |
| 171 | 150 | |
| 172 | - // Allowed tags | |
| 173 | - $allowed_tags = array_merge(wp_kses_allowed_html('post'), helper_svg_allowed_html()); | |
| 151 | + $allowed_tags = wp_kses_allowed_html('post'); | |
| 152 | + $allowed_tags = array_merge($allowed_tags, helper_svg_allowed_html()); | |
| 174 | 153 | |
| 175 | - // Elementor-friendly: return instead of echo | |
| 176 | - return '<div class="eel-breadcrumb"><div class="breadcrumb-path">' . wp_kses($output, $allowed_tags) . '</div></div>'; | |
| 154 | + echo '<div class="eel-breadcrumb"><div class="breadcrumb-path">' . wp_kses($output, $allowed_tags) . '</div></div>'; | |
| 177 | 155 | } |
| 178 | 156 | } |
| 157 | +?> | |