-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl-header-breadcrumbs.php
362 lines (324 loc) · 12.4 KB
/
ctl-header-breadcrumbs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
function display_breadcrumb() {
global $post;
$separator = ' <span class="breadcrumb-separator">/</span> ';
echo '<div class="breadcrumb">';
echo '<a href="' . home_url() . '">Home</a>' . $separator;
if ( is_archive() || is_search() ) {
echo '<span class="breadcrumb-current">';
echo get_the_archive_title();
echo '</span>';
} elseif ( is_404() ) {
echo '<span class="breadcrumb-current">Error 404</span>';
} elseif ( is_single() ) {
$categories = get_the_category();
$category = $categories[0];
$cat_id = $category->cat_ID;
$cat_link = get_category_link( $cat_id );
$cat_name = $category->name;
echo '<a href="' . $cat_link . '">' . $cat_name . '</a>' . $separator;
echo '<span class="breadcrumb-current">';
the_title();
echo '</span>';
} elseif ( is_page() ) {
$ancestors = get_post_ancestors( $post->ID );
if ( $ancestors ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
echo '<a href="' . get_permalink( $ancestor ) . '">' . get_the_title( $ancestor ) . '</a>' . $separator;
}
}
echo '<span class="breadcrumb-current">';
the_title();
echo '</span>';
} elseif ( is_category() ) {
$category = get_queried_object();
$cat_id = $category->cat_ID;
$cat_link = get_category_link( $cat_id );
$cat_name = $category->name;
echo '<a href="' . home_url() . '">Blog</a>' . $separator;
echo '<a href="' . $cat_link . '">' . $cat_name . '</a>';
} else {
echo '<span class="breadcrumb-current">';
the_title();
echo '</span>';
}
echo '</div>';
}
function get_header_breadcrumbs() {
if ( get_theme_mod( 'mytheme_breadcrumbs_toggle', false ) )
{
if ( !is_home() && !is_front_page() ) {
echo '<div id="crumb" class="center-breadcrumb innerA clear-formatting">';
echo '<nav aria-label="breadcrumb"><ol class="breadcrumb">';
echo '<li class="breadcrumb-item"><a href="'.home_url('/').'">'.__('Home').'</a></li>';
echo '<li class="breadcrumb-item"> > </li>';
if ( is_category() || is_single() ) {
echo '<li class="breadcrumb-item">';
the_category(' </li><li class="breadcrumb-item"> ');
if ( is_single() ) {
echo '</li><li class="breadcrumb-item">';
the_title();
echo '</li>';
}
} elseif ( is_page() ) {
echo '<li class="breadcrumb-item">';
echo the_title();
echo '</li>';
}
echo '</ol></nav>';
echo '</div>';
}
}
}
/**
* Echo or return a formatted list of breadcrumbs.
*
* @param array $args An array of arguments to controll the output of the
* function.
* @return string The breadcrumb list.
*/
function get_breadcrumbs($args = array())
{
if ( get_theme_mod( 'mytheme_breadcrumbs_toggle', false ) )
{
global $post;
if (is_string($args)) {
parse_str($args, $args);
}
// Set up defaults.
$defaults = array(
'separator' => ' > ',
'linkFinal' => false,
'echo' => true,
'printOnHome' => true,
'before' => '',
'after' => '',
);
// Merge the defaults with the parameters.
$options = array_merge($defaults, (array)$args);
// Initialise the trail with the current post.
$trail = array($post);
// Initialise the output.
$output = '';
$currentCategory = 0;
if (is_front_page() == true && $options['printOnHome'] == false) {
/**
* If this is the front page and the option to prevent priting on the
* home page is disabled then echo or return the empty string depending
* on the echo option.
*/
if ($options['echo'] == true) {
echo $output;
}
return $output;
}
if (is_page()) {
// If the current page is a page.
$parent = $post;
while ($parent->post_parent) {
$parent = get_post($parent->post_parent);
array_unshift($trail, $parent);
}
} elseif (is_category()) {
// The current page is a category page.
$trail = array();
$currentCategory = get_query_var('cat');
$category = get_category($currentCategory);
while ($category->category_parent > 0) {
array_unshift($trail, $category);
$category = get_category($category->category_parent);
}
// Add the final category to the trail.
array_unshift($trail, $category);
} else {
// The current page will be a post or set of posts.
$path = explode('/', $_SERVER['REQUEST_URI']);
$path = array_filter($path);
while (count($path) > 0) {
$page = get_page_by_path(implode('/', $path));
if ($page != NULL) {
array_unshift($trail, $page);
}
array_pop($path);
}
if (count($trail) == 1) {
// No pages found in path, try using categories.
$category = get_the_category();
$category = $category[0];
while ($category->category_parent > 0) {
array_unshift($trail, $category);
$category = get_category($category->category_parent);
}
array_unshift($trail, $category);
}
}
$show_on_front = get_option('show_on_front');
if ('posts' == $show_on_front) {
// The home page is a list of posts so just call it Home.
$output .= '<li class="breadcrumb-item" id="breadcrumb-0"><a href="' . get_bloginfo('home') . '" title="Home">Home</a></li>' . "\n"; // home page link
} else {
// Otherwise the front page is a page so get the page name.
$page_on_front = get_option('page_on_front');
$home_page = get_post($page_on_front);
$output .= '<li class="breadcrumb-item" id="breadcrumb-' . $home_page->ID . '"><a href="' . get_bloginfo('home') . '" title="' . $home_page->post_title . '">' . $home_page->post_title . '</a></li>' . "\n"; // home page link
if ($trail[0]->ID == $page_on_front) {
array_shift($trail);
}
}
if (is_front_page() == false) {
// If we aren't on the home page then construct the output.
foreach ($trail as $key => $page) {
// Every item in the trail will be either a post/page object or a category.
if (count($trail) - 1 == $key && $options['linkFinal'] == false) {
// If we are on the last page and the option to link the final link is true.
if (isset($page->post_title)) {
$output .= '<li class="breadcrumb-item" id="breadcrumb-' . $page->ID . '">' . $options['separator'] . ' ' . $page->post_title . '</li>' . "\n";
} elseif (isset($page->cat_name)) {
$output .= '<li class="breadcrumb-item" id="breadcrumb-cat-' . $page->term_id . '">' . $options['separator'] . ' ' . $page->cat_name . '</li>' . "\n";
}
} else {
// Create the link to the page or category
if (isset($page->post_title)) {
$output .= '<li class="breadcrumb-item" id="breadcrumb-' . $page->ID . '">' . $options['separator'] . '<a href="' . get_page_link($page->ID) . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>' . "\n";
} elseif (isset($page->cat_name)) {
$output .= '<li class="breadcrumb-item" id="breadcrumb-cat-' . $page->term_id . '">' . $options['separator'] . '<a href="' . get_category_link($page->term_id) . '" title="' . $page->cat_name . '">' . $page->cat_name . '</a></li>' . "\n";
}
}
}
}
// Finish off the html of the ul
$output = "<ul>\n" . $output . "</ul>\n";
// Add other elements
$output = $options['before'] . $output . $options['after'];
if ($options['echo'] == true) {
// Print out the $output variable.
echo '<div id="crumb" class="center-breadcrumb clear-formatting">';
echo '<nav aria-label="breadcrumb">'; // ol
echo $output;
echo '</nav>';
echo '</div>';
}
// Return
return $output;
}
}
/////////////////////////////////////////
/* Customize Appearance Options header */
/////////////////////////////////////////
function header_breadcrumbs( $wp_customize ) {
/* Create section */
$wp_customize->add_section('header_breadcrumbs', array(
'title' => __('Header - Bread Crumbs', 'qbytesworld_WordPress'),
'priority' => 165,
));
/* ************************************************************ */
/* ************************************************************ */
/* ************************************************************ */
// Add setting for breadcrumbs
$wp_customize->add_setting( 'mytheme_breadcrumbs_toggle', array(
'default' => true,
'transport' => 'refresh',
) );
// Add control for breadcrumbs
$wp_customize->add_control( 'mytheme_breadcrumbs_toggle', array(
'section' => 'header_breadcrumbs',
'settings' => 'mytheme_breadcrumbs_toggle',
'label' => __( 'Display Breadcrumbs', 'mytheme' ),
'description' => __( 'Toggle breadcrumbs on or off.', 'mytheme' ),
'type' => 'checkbox',
) );
/* ************************************************************ */
// Add checkbox to hide title & tagline
$wp_customize->add_setting( 'header_breadcrumbs_sub_checkbox', array(
'default' => false,
'transport' => 'refresh',
) );
$wp_customize->add_control( 'header_breadcrumbs_sub_checkbox', array(
'label' => __( 'Hide Title & Tagline', 'qbytesworld_WordPress' ),
'section' => 'header_breadcrumbs',
'settings' => 'header_breadcrumbs_checkbox',
'description' => __( 'True/False', 'qbytesworld_WordPress' ),
'type' => 'checkbox',
) );
/* Button color */
$wp_customize->add_setting('header_breadcrumbs_color', array(
'default' => '#004C87',
'transport' => 'refresh',
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_breadcrumbs_color_control', array(
'label' => __('Button Color', 'qbytesworld_WordPress'),
'section' => 'header_breadcrumbs',
'settings' => 'header_breadcrumbs_color',
'description'=> __( 'Color for Menu Item', 'qbytesworld_WordPress' ),
) ) );
/* Button hover color */
$wp_customize->add_setting('header_breadcrumbs_color_hover', array(
'default' => '#004C87',
'transport' => 'refresh',
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_breadcrumbs_color_hover_control', array(
'label' => __('Button Hover Color', 'qbytesworld_WordPress'),
'section' => 'header_breadcrumbs',
'settings' => 'header_breadcrumbs_color_hover',
'description'=> __( 'Mouse Menu Item Hover color', 'qbytesworld_WordPress' ),
) ) );
/* Button hover color */
$wp_customize->add_setting('header_breadcrumbs_background_color', array(
'default' => '#004C87',
'transport' => 'refresh',
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_breadcrumbs_background_color_control', array(
'label' => __('Button Background Color', 'qbytesworld_WordPress'),
'section' => 'header_breadcrumbs',
'settings' => 'header_breadcrumbs_background_color',
'description'=> __( 'Background Item color', 'qbytesworld_WordPress' ),
) ) );
}
add_action('customize_register', 'header_breadcrumbs');
///////////////////////////////////////////////
/* Output Customize CSS to support controls */
///////////////////////////////////////////////
function header_breadcrumbs_css() { ?>
<style type="text/css">
/* breadcrumbs */
/* header div.clear-formatting * {
all: unset;
margin: 0;
padding: 0;
font-family: inherit;
} */
div.header-content{
background: <?php echo get_theme_mod('main_content_background_top'); ?>
}
header div.center-breadcrumb{
background-color: <?php echo get_theme_mod('header_breadcrumbs_background_color'); ?>;
display: flex;
margin-top:20px;
width:70%;
justify-content: center;
align-items: center;
border: <?php echo get_theme_mod('main_content_border_size'); ?>px solid <?php echo get_theme_mod('header_order_border_color'); ?>;
box-shadow: <?php echo get_theme_mod('main_content_dropshadow_offset'); ?>px <?php echo get_theme_mod('main_content_dropshadow_offset'); ?>px <?php echo get_theme_mod('main_article_dropshadow_size'); ?>px <?php echo get_theme_mod('main_article_dropshadow');?>;
border-radius: 15px;
}
header div.center-breadcrumb nav ul li{
color: <?php echo get_theme_mod('header_breadcrumbs_color_hover'); ?>;
display: inline;
}
li.breadcrumb-item{
padding: 0;
}
li.breadcrumb-item a,
nav.breadcrumb ul li.breadcrumb-item a{
font-size: 18px !important;
color: <?php echo get_theme_mod('header_breadcrumbs_color'); ?>;
}
li.breadcrumb-item a:hover,
nav.breadcrumb ul li.breadcrumb-item a:hover{
color: <?php echo get_theme_mod('header_breadcrumbs_color_hover'); ?>;
}
</style>
<?php }
add_action('wp_head', 'header_breadcrumbs_css');
?>