How to create a breadcrumb navigation? How to modify the Robin breadcrumb navigation?

How to create a breadcrumb navigation? How to modify the Robin breadcrumb navigation?

Each theme template has its own differences. The editor Dongguan SEO is also trying different theme templates. When I was making the Robin template two days ago, I wanted to modify the breadcrumb navigation. It took me a lot of effort to get it done. In view of this, today I will share my modification methods and steps with everyone. Let’s take a look together!

How to create a breadcrumb navigation? How to modify the Robin breadcrumb navigation?

To modify the breadcrumb navigation, we first need to know where the breadcrumb navigation code is. Since Robin has its own breadcrumb navigation, but this is not conducive to SEO optimization, it needs to be modified.

1. Appearance-Edit-header.php
Copy and save the following code:
<?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>

2. Appearance-Edit-function.php
Add the following code to your theme's functions.php:
/**
* Add breadcrumb navigation to WordPress
* http://www.yunmajp.com
*/
function cmp_breadcrumbs() {
$delimiter = '»'; // delimiter
$before = '<span class="current">'; // Insert before the current link
$after = '</span>'; // insert after the current link
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '<div itemscope itemtype="http://schema.org/WebPage" id="crumbs">'.__( 'Current location:' , 'cmp' );
global $post;
$homeLink = home_url();
echo ' <a itemprop="breadcrumb" href="' . $homeLink . '">' . __( 'Dongguan SEO Homepage' , 'cmp' ) . '</a> ' . $delimiter . ' ';
if ( is_category() ) { // Category archive
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0){
$cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code);
}
echo $before . '' . single_cat_title('', false) . '' . $after;
} elseif ( is_day() ) { // day archive
echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a itemprop="breadcrumb" href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('d') . $after;
} elseif ( is_month() ) { // Monthly archive
echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('F') . $after;
} elseif ( is_year() ) { // year archive
echo $before . get_the_time('Y') . $after;
} elseif ( is_single() && !is_attachment() ) { // Article
if ( get_post_type() != 'post' ) { // Custom post type
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo '<a itemprop="breadcrumb" href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} else { // Article post
$cat = get_the_category(); $cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code);
echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) { // Attachment
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo '<a itemprop="breadcrumb" href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) { // Page
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) { // Parent page
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a itemprop="breadcrumb" href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} elseif ( is_search() ) { // Search results
echo $before ;
printf( __( 'Search Results for: %s', 'cmp' ), get_search_query() );
echo $after;
} elseif ( is_tag() ) { //Tag archive
echo $before ;
printf( __( 'Tag Archives: %s', 'cmp' ), single_tag_title( '', false ) );
echo $after;
} elseif ( is_author() ) { // Author archive
global $author;
$userdata = get_userdata($author);
echo $before ;
printf( __( 'Author Archives: %s', 'cmp' ), $userdata->display_name );
echo $after;
} elseif ( is_404() ) { // 404 page
echo $before;
_e( 'Not Found', 'cmp' );
echo $after;
}
if ( get_query_var('paged') ) { // Paging
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo sprintf( __( '( Page %s )', 'cmp' ), get_query_var('paged') );
}
echo '</div>';
}
}

Note: There are two places in the code in function that you need to modify yourself. Just save it. Then the breadcrumb navigation modification is complete. If you still don't understand, please contact the editor QQ: 3196077147

<<:  What is the preferred domain? How do I set my preferred domain?

>>:  How to make robots? How to write robots?

Recommend

Baby Safe Medication Guide Upgraded Disease Care Guide

Baby's safe medication guide upgraded version...

Douyin e-commerce standardized product selection guide (recommended collection)

If you want to do well in Douyin live streaming s...

Is space junk a serious problem? What would happen if we left it alone?

We collect the garbage from our daily work and li...

Dogs are getting more and more used to drinking milk as their owners do.

Produced by: Science Popularization China Produce...

Luxury brand Montblanc launches smart watch strap, starting at £250

There are many luxury brands in the smart hardwar...

Hong Raiders Value Season Ranch Ranch A Intensive Training Camp

Hong Raiders Value Season Ranch Ranch A Intensive...

Side job: 2 hours a day working on Pinduoduo and earning 3,000 yuan a month

Side job: 2 hours a day working on Pinduoduo and ...

50 questions about Douyin influencer promotion!

50 questions about Douyin influencer promotion ! ...

How to increase product user growth? Share the 8-step plan!

The author uses a real case to explain how to bui...