Advertisement
Oscarobians

Breadcrumbs snippet

May 6th, 2023 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | Software | 0 0
  1. /*
  2.     To call the function on the front end use the following:
  3.     <?php
  4.         the_breadcrumbs();
  5.     ?>
  6. */
  7.  
  8. function the_breadcrumbs() {
  9.   $breadcrumb_separator = ' > ';
  10.   if (!is_home()) {
  11.     echo '<a href="' . home_url('/') . '">Home</a><span>' . $breadcrumb_separator . '</span>';
  12.    
  13.     // Check if we're on a category-based archive page
  14.     if (is_category()) {
  15.       $categories = get_the_category();
  16.       $output = '';
  17.      
  18.       if ($categories) {
  19.         foreach ($categories as $category) {
  20.           $output .= '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a><span>' . $breadcrumb_separator . '</span>';
  21.         }
  22.         echo trim($output, '<span>' . $breadcrumb_separator . '</span>');
  23.       }
  24.      
  25.       // Check if we're on a date-based archive page
  26.     } elseif (is_date()) {
  27.       if (is_day()) {
  28.         echo '<span>' . get_the_date('F j, Y') . $breadcrumb_separator .'</span>';
  29.       } elseif (is_month()) {
  30.         echo '<span>' . get_the_date('F Y') . $breadcrumb_separator . '</span>';
  31.       } else {
  32.         echo '<span>' . get_the_date('Y') . $breadcrumb_separator . '</span>';
  33.       }
  34.      
  35.       // Check if we're on a tag-based archive page
  36.     } elseif (is_tag()) {
  37.       $tag = single_tag_title('', false);
  38.       echo '<span>' . $tag . '</span>';
  39.      
  40.       // Check if we're on a custom post type archive page
  41.     } elseif (is_post_type_archive()) {
  42.       echo '<span>' . post_type_archive_title('', false) . '</span>';
  43.      
  44.       // Check if we're on a search results page
  45.     } elseif (is_search()) {
  46.       echo '<span>Search Results for "' . get_search_query() . '"</span>';
  47.     } else {
  48.       // For any other page, just display the page title
  49.       the_title();
  50.     }
  51.   } else {
  52.     echo 'Home';
  53.   }
  54. }
  55.  
Tags: wordpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement