How to Add Breadcrumbs Schema without a plugin

In WordPress, breadcrumbs are a type of navigation element that helps users understand the structure of your website and their current location within it.

They are usually displayed as a series of links at the top of a page, separated by a delimiter such as a “>” symbol.

For example, if you have a website with a structure like “Home > Blog > Category > Post,” the breadcrumb trail for a particular post might look like this:

Home > Blog > Category > Post

Breadcrumbs provide a useful way for users to quickly navigate back to a higher level in the website hierarchy, and can also help search engines to understand the structure of your website and its content.

These are some code snippets to generate breadcrumbs schema without a plugin.


Breadcrumbs Schema by SlimSEO

This is the breadcrumbs schema that SLIM SEO generates even when I am not displaying breadcrumbs anywhere on my site.

Breadcrumbs Schema - Slim SEO

You will also have the option to generate the breadcrumbs schema without displaying breadcrumbs anywhere on your site.

Code to Generate Breadcrumbs Schema

This is what will generate the schema for the breadcrumbs. This will not add breadcrumbs to your site’s front end.

Home>> category>> Post

function get_breadcrumb_json_ld() {
  $home_url = get_home_url();
  $post_categories = get_the_category();
  $first_category = $post_categories[0];
  $category_url = get_category_link($first_category->term_id);
  $post_title = get_the_title();
  $post_url = get_the_permalink();

  $json_ld = array(
    '@context' => 'https://schema.org',
    '@type' => 'BreadcrumbList',
	'@name' => 'Breadcrumbs',
    'itemListElement' => array(
      array(
        '@type' => 'ListItem',
        'position' => 1,
		'name' => 'Home',
        'item' => array(
          '@id' => $home_url,
          
        ),
      ),
      array(
        '@type' => 'ListItem',
        'position' => 2,
		'name' => $first_category->name,
        'item' => array(
          '@id' => $category_url,
          
        ),
      ),
      array(
        '@type' => 'ListItem',
        'position' => 3,
		'name' => $post_title,
        'item' => array(
          '@id' => $post_url,
          
        ),
      ),
    ),
  );

  return json_encode($json_ld, JSON_UNESCAPED_SLASHES);
}

This is limited to Home and category, similar to what SLIM SEO does

Home>> category>>

function get_breadcrumb_json_ld() {
  $home_url = get_home_url();
  $post_categories = get_the_category();
  $first_category = $post_categories[0];
  $category_url = get_category_link($first_category->term_id);
  $post_title = get_the_title();
  $post_url = get_the_permalink();

$json_ld = array(
  '@context' => 'https://schema.org',
  '@type' => 'BreadcrumbList',
  '@name' => 'Breadcrumbs',
  'itemListElement' => array(
    array(
      '@type' => 'ListItem',
      'position' => 1,
      'name' => 'Home',
      'item' => array(
        '@id' => $home_url,
      ),
    ),
    array(
      '@type' => 'ListItem',
      'position' => 2,
      'name' => $first_category->name,
      'item' => array(
        '@id' => $category_url,
      ),
    ),
  ),
);

  return json_encode($json_ld, JSON_UNESCAPED_SLASHES);
}

This function will add the schema generated for the breadcrumbs to the footer in all posts.

function output_breadcrumb_json_ld() {
  if ( is_single() ) {
    $json_ld = get_breadcrumb_json_ld();
    echo '<script type="application/ld+json">' . $json_ld . '</script>';
  }
}
add_action('wp_footer', 'output_breadcrumb_json_ld');

Disclaimer

This is not the perfect solution to replace the schema generated by SEO Plugins

I haven’t checked how different SEO plugins accomplish this and if there are differences in the approach used by the developers.

You can play with these code snippets in a local environment and make adjustments to them.

More Code Snippets

There are more code snippets where this came from.

Manuel Campos, English Professor

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica