面包屑导航函数Breadcrumb


Updated on: September 1, 2020 in Wordpress by

/*=============================================
=            BREADCRUMBS			            =
=============================================*/

//  to include in functions.php
function the_breadcrumb() {

    $sep = '    »   ';

    if (!is_front_page()) {
	
	// Start the breadcrumb with a link to your homepage
        echo '<ol class="breadcrumb no-border">';
        echo '<li class="breadcrumb-items light"><a class="link link-light-primary size-14" data-hover="' . get_the_title(get_page_by_path( 'home' )) . '" href="';
        echo get_option( 'home' );
        echo '">';
        echo get_the_title(get_page_by_path( 'home' ));
        echo '</a></li>' . $sep;
	
	// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
        if (is_category() || is_single() ){
        	echo '<li class="breadcrumb-items light">';
            the_category('title_li=');
            echo '</li>';
        } elseif (is_archive() || is_single()){
            if ( is_day() ) {
                printf( __( '%s', 'webagency' ), get_the_date() );
            } elseif ( is_month() ) {
                printf( __( '%s', 'webagency' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'webagency' ) ) );
            } elseif ( is_year() ) {
                printf( __( '%s', 'webagency' ), get_the_date( _x( 'Y', 'yearly archives date format', 'webagency' ) ) );
            } else {
                _e( 'Blog Archives', 'webagency' );
            }
        }
	
	// If the current page is a single post, show its title with the separator
        if (is_single()) {
            echo $sep;
            the_title();
        }
	
	// If the current page is a static page, show its title.
        if (is_page()) {
            echo the_title();
        }
	
	// if you have a static page assigned to be you posts list page. It will find the title of the static page and display it. i.e Home >> Blog
        if (is_home()){
            global $post;
            $page_for_posts_id = get_option('page_for_posts');
            if ( $page_for_posts_id ) { 
                $post = get_page($page_for_posts_id);
                setup_postdata($post);
                the_title();
                rewind_posts();
            }
        }

        echo '</ol>';
    }
}