<?php
/* Breadcrumb */
function the_breadcrumb() {
    $sep = '<span class="separator"></span>';
    if (!is_front_page()) {
        echo '<div class="breadcrumbs alignwide">';
        echo '<a href="' . get_option('home') . '" class="link--noline">Home</a>' . $sep;

        if (is_category() || is_single()) {
            // Display category hierarchy
            if (is_single()) {
                $categories = get_the_category();
                if ($categories) {
                    $category = $categories[0];
                    $ancestors = get_ancestors($category->term_id, 'category');
                    foreach (array_reverse($ancestors) as $ancestor) {
                        $ancestor_category = get_category($ancestor);
                        echo '<a href="' . get_category_link($ancestor_category->term_id) . '">' . $ancestor_category->name . '</a>' . $sep;
                    }
                    echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>' . $sep;
                }
                echo '<span>' . get_the_title() . '</span>';
            } elseif (is_category()) {
                $category = get_queried_object();
                $ancestors = get_ancestors($category->term_id, 'category');
                foreach (array_reverse($ancestors) as $ancestor) {
                    $ancestor_category = get_category($ancestor);
                    echo '<a href="' . get_category_link($ancestor_category->term_id) . '">' . $ancestor_category->name . '</a>' . $sep;
                }
                echo '<span>' . $category->name . '</span>';
            }
        } elseif (is_archive()) {
            if (is_day()) {
                echo '<span>' . get_the_date() . '</span>';
            } elseif (is_month()) {
                echo '<span>' . get_the_date(_x('F Y', 'monthly archives date format')) . '</span>';
            } elseif (is_year()) {
                echo '<span>' . get_the_date(_x('Y', 'yearly archives date format')) . '</span>';
            } else {
                echo '<span>Blog Archives</span>';
            }
        } elseif (is_page()) {
            global $post;
            if ($post->post_parent) {
                $ancestors = get_post_ancestors($post->ID);
                foreach (array_reverse($ancestors) as $ancestor) {
                    $ancestor_post = get_post($ancestor);
                    echo '<a href="' . get_permalink($ancestor_post->ID) . '">' . get_the_title($ancestor_post->ID) . '</a>' . $sep;
                }
            }
            echo '<span>' . get_the_title() . '</span>';
        } elseif (is_home()) {
            $page_for_posts_id = get_option('page_for_posts');
            if ($page_for_posts_id) {
                $post = get_post($page_for_posts_id);
                setup_postdata($post);
                echo '<span>' . get_the_title() . '</span>';
                rewind_posts();
            }
        }

        echo '</div>';
    }
}
