• Resolved wordpresskid

    (@wordpresskid)


    Hello Ben,

    and thank you for your great work and theme !

    I try to implement your breadcrumbs into twentyfifteen and your code is working out of the box ! I just have to write the appropriate css.

    There is just a little problem with the under-pages of the home page. In that case the breadcrumbs code add home twice, once as defined in the snippet and second with the original name of the home page, eg: home > original home name > under-page

    Could you please advice me which wordpress function and where to use it in order to skip showing home twice, when an under-page of the home page is active?

    I’m still a beginner with wordpress, even if I can understand or check your php code.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Theme Author Ben Sibley

    (@bensibley)

    Glad you like it!

    Try modifying the is_singular( 'page' ) section on lines 46-57. You’ll see it checks if the page has a parent page there. I think you’ll want to check if the parent page is the homepage, and ignore it if it is.

    Thread Starter wordpresskid

    (@wordpresskid)

    Thank you Ben for your advice,

    I found the magic wordpress function to return the ID of the home page: get_option(‘page_on_front’) :). Thank you again for your help and for all other wordpress beginners, who want to implement breadcrumbs in a theme without using a plugin, then put this in functions.php:

    // breadcrumbs (Ben Sibley,  https://www.competethemes.com)
    if ( ! function_exists( 'ct_ignite_breadcrumbs' ) ) {
    	function ct_ignite_breadcrumbs( $args = array() ) {
    
    		if ( is_front_page() ) {
    			return;
    		}
    		if ( get_theme_mod( 'ct_ignite_show_breadcrumbs_setting' ) == 'no' ) {
    			return;
    		}
    
    		global $post;
    		$defaults  = array(
    			'separator_icon'      => '>',
    			'breadcrumbs_id'      => 'breadcrumbs',
    			'breadcrumbs_classes' => 'breadcrumb-trail breadcrumbs',
    			'home_title'          => 'Home'
    		);
    		$args      = apply_filters( 'ct_ignite_breadcrumbs_args', wp_parse_args( $args, $defaults ) );
    		$separator = '<span class="separator"> ' . esc_attr( $args['separator_icon'] ) . ' </span>';
    
    		/***** Begin Markup *****/
    
    		// Open the breadcrumbs
    		$html = '<div id="' . esc_attr( $args['breadcrumbs_id'] ) . '" class="' . esc_attr( $args['breadcrumbs_classes'] ) . '">';
    
    		// Add Homepage link & separator (always present)
    		$html .= '<span class="item-home"><a class="bread-link bread-home" href="' . get_home_url() . '" title="' . esc_attr( $args['home_title'] ) . '">' . esc_attr( $args['home_title'] ) . '</a></span>';
    		$html .= $separator;
    
    		// Post
    		if ( is_singular( 'post' ) ) {
    
    			$category = get_the_category();
    			$category_values = array_values( $category );
    			$last_category = end( $category_values );
    			$cat_parents = rtrim( get_category_parents( $last_category->term_id, true, ',' ), ',' );
    			$cat_parents = explode( ',', $cat_parents );
    
    			foreach ( $cat_parents as $parent ) {
    				$html .= '<span class="item-cat">' . wp_kses( $parent, wp_kses_allowed_html( 'a' ) ) . '</span>';
    				$html .= $separator;
    			}
    			$html .= '<span class="item-current item-' . $post->ID . '"><span class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</span></span>';
    		} elseif ( is_singular( 'page' ) ) {
    
    			if ( $post->post_parent ) {
    				$parents = get_post_ancestors( $post->ID );
    				$parents = array_reverse( $parents );
    
    				foreach ( $parents as $parent ) {
    					if ($parent != get_option('page_on_front')) {
    						$html .= '<span class="item-parent item-parent-' . esc_attr( $parent ) . '"><a class="bread-parent bread-parent-' . esc_attr( $parent ) . '" href="' . esc_url( get_permalink( $parent ) ) . '" title="' . get_the_title( $parent ) . '">' . get_the_title( $parent ) . '</a></span>';
    						$html .= $separator;
    					}
    				}
    			}
    			$html .= '<span class="item-current item-' . $post->ID . '"><span title="' . get_the_title() . '"> ' . get_the_title() . '</span></span>';
    		} elseif ( is_singular( 'attachment' ) ) {
    
    			$parent_id        = $post->post_parent;
    			$parent_title     = get_the_title( $parent_id );
    			$parent_permalink = esc_url( get_permalink( $parent_id ) );
    
    			$html .= '<span class="item-parent"><a class="bread-parent" href="' . esc_url( $parent_permalink ) . '" title="' . esc_attr( $parent_title ) . '">' . esc_attr( $parent_title ) . '</a></span>';
    			$html .= $separator;
    			$html .= '<span class="item-current item-' . $post->ID . '"><span title="' . get_the_title() . '"> ' . get_the_title() . '</span></span>';
    		} elseif ( is_singular() ) {
    
    			$post_type         = get_post_type();
    			$post_type_object  = get_post_type_object( $post_type );
    			$post_type_archive = get_post_type_archive_link( $post_type );
    
    			$html .= '<span class="item-cat item-custom-post-type-' . esc_attr( $post_type ) . '"><a class="bread-cat bread-custom-post-type-' . esc_attr( $post_type ) . '" href="' . esc_url( $post_type_archive ) . '" title="' . esc_attr( $post_type_object->labels->name ) . '">' . esc_attr( $post_type_object->labels->name ) . '</a></span>';
    			$html .= $separator;
    			$html .= '<span class="item-current item-' . $post->ID . '"><span class="bread-current bread-' . $post->ID . '" title="' . $post->post_title . '">' . $post->post_title . '</span></span>';
    		} elseif ( is_category() ) {
    
    			$parent = get_queried_object()->category_parent;
    
    			if ( $parent !== 0 ) {
    
    				$parent_category = get_category( $parent );
    				$category_link   = get_category_link( $parent );
    
    				$html .= '<span class="item-parent item-parent-' . esc_attr( $parent_category->slug ) . '"><a class="bread-parent bread-parent-' . esc_attr( $parent_category->slug ) . '" href="' . esc_url( $category_link ) . '" title="' . esc_attr( $parent_category->name ) . '">' . esc_attr( $parent_category->name ) . '</a></span>';
    				$html .= $separator;
    			}
    			$html .= '<span class="item-current item-cat"><span class="bread-current bread-cat" title="' . $post->ID . '">' . single_cat_title( '', false ) . '</span></span>';
    		} elseif ( is_tag() ) {
    			$html .= '<span class="item-current item-tag"><span class="bread-current bread-tag">' . single_tag_title( '', false ) . '</span></span>';
    		} elseif ( is_author() ) {
    			$html .= '<span class="item-current item-author"><span class="bread-current bread-author">' . get_queried_object()->display_name . '</span></span>';
    		} elseif ( is_day() ) {
    			$html .= '<span class="item-current item-day"><span class="bread-current bread-day">' . get_the_date() . '</span></span>';
    		} elseif ( is_month() ) {
    			$html .= '<span class="item-current item-month"><span class="bread-current bread-month">' . get_the_date( 'F Y' ) . '</span></span>';
    		} elseif ( is_year() ) {
    			$html .= '<span class="item-current item-year"><span class="bread-current bread-year">' . get_the_date( 'Y' ) . '</span></span>';
    		} elseif ( is_archive() ) {
    			$custom_tax_name = get_queried_object()->name;
    			$html .= '<span class="item-current item-archive"><span class="bread-current bread-archive">' . esc_attr( $custom_tax_name ) . '</span></span>';
    		} elseif ( is_search() ) {
    			$html .= '<span class="item-current item-search"><span class="bread-current bread-search">Search results for: ' . get_search_query() . '</span></span>';
    		} elseif ( is_404() ) {
    			$html .= '<span>' . __( 'Error 404', 'ignite' ) . '</span>';
    		} elseif ( is_home() ) {
    			$html .= '<span>' . get_the_title( get_option( 'page_for_posts' ) ) . '</span>';
    		}
    
    		$html .= '</div>';
    		$html = apply_filters( 'ct_ignite_breadcrumbs_filter', $html );
    
    		echo wp_kses_post( $html );
    	}
    }

    and call the function in a child theme file like header.php or content-page.php or wherever you want to have the breadcrumbs:

    <?php ct_ignite_breadcrumbs(); ?>

    and don’t forget to customize your breadcrumbs css classes ??

    Have a nice day Ben and maybe I’ll consider one of your theme for my next project, they are so clean and modern ??

    Marius

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Breadcrumbs for underpages of the home page’ is closed to new replies.