jrobinsonc
6/1/2015 - 2:56 PM

Wordpress helper: get_breadcrumbs.

Wordpress helper: get_breadcrumbs.

<?php

/**
 * get_breadcrumbs
 * 
 * @author JoseRobinson.com
 * @link https://gist.github.com/jrobinsonc/f93d9462ebea6be138d0
 * @param string $home_label
 * @param string $separator
 * @return string
 */
function get_breadcrumbs($home_label, $separator)
{
    global $post;
    
    $output = '';


    if (!is_front_page())
        $output .= sprintf('<a href="%s">%s</a>', get_option('home'), $home_label);
    
    if (is_page())
    {
        if (!is_front_page())
        {
            if($post->post_parent)
            {
                $anc = get_post_ancestors( $post->ID );
                $anc_link = get_page_link( $post->post_parent );
                $anc = array_reverse($anc);

                foreach ( $anc as $ancestor )
                    $output .= sprintf(' %1$s <a href="%2$s">%3$s</a> ', $separator, get_permalink($ancestor), get_the_title($ancestor));
            } 
            
            $output .= sprintf(' %s <span class="breadcrumb_last">%s</span>', $separator, get_the_title());
        }
        
    }
    elseif (is_category() || is_single())
    {
        $cats = get_the_category( $post->ID );

        foreach ( $cats as $cat )
        {
            $output .= sprintf(' %1$s <a href="%2$s">%3$s</a> ', $separator, get_category_link($cat->term_id), $cat->name);
            break;
        }
    }
    elseif (is_search()) 
    {
        $output .= sprintf(' %s <span class="breadcrumb_last">Resultados</span>', $separator);
    }
    // elseif (is_tag()) {$output .= single_tag_title('', false);}
    // elseif (is_day()) {$output .= 'Archive: ' . get_the_time('F jS, Y');}
    // elseif (is_month()) {$output .= 'Archive: ' . get_the_time('F, Y');}
    // elseif (is_year()) {$output .= 'Archive: ' . get_the_time('Y');}
    // elseif (is_author()) {$output .= 'Author\'s archive: ';}
    // elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {$output .= 'Blog Archive: ';}
    
    return $output;

}