Add breadcrumb to archives

Hi,
Previously someone gave me this code, pasted below, to add a breadcrumb to my posts that brought you back to the blog. I want to also add the same breadcrumb to my category archives page. Like this one:http://2e6.274.myftpupload.com/category/fat-cat-news/ so that people can just go back to the blog. How would I edit or this code for this? TIA! Rena

add_action( 'x_before_view_integrity_content', 'print_breadcrumb_for_posts' );
function print_breadcrumb_for_posts(){
	if(is_singular( 'post' )){
		?>
		<div class="custom-breadcrumb">
			<a href="<?php echo get_permalink( get_option('page_for_posts') ); ?>">Return to Blog</a>
		</div>
		<?php
	}
}

Hi Rena,

Thanks for reaching out.

It should be just the same code, but change this line

if(is_singular( 'post' )){

to this

if(is_singular( 'post' ) || is_category() ){

Hope this helps :slight_smile:

Hm… I added this, below the code that is set for the “return to blog” on the posts. but my site goes blank when I have it in. Do I have it right?

// Add Return to Blog to Categories
// =============================================================================

add_action( 'x_before_view_integrity_content', 'print_breadcrumb_for_posts' );
function print_breadcrumb_for_posts(){
	if(is_singular( 'post' ) || is_category() ){
		?>
		<div class="custom-breadcrumb">
			<a href="<?php echo get_permalink( get_option('page_for_posts') ); ?>">&#60; Return to Blog</a>
		</div>
		<?php
	}
}

Hi Rena,

Would you mind providing the entire content of your child theme’s functions.php while that code is added?

I just tried that code and it works perfectly on my end

Thanks!

I just tried it again to be sure, but still just a white screen… here it is…

Im not sure how I send code over :confused:

Hi Rena,

Ah, you have added it again causing multiple existence of the function, as a recommendation, you should only change this line of that existing code

if(is_singular( 'post' )){

to this

if(is_singular( 'post' ) || is_category() ){

Don’t add the same code, just edit the existing once :slight_smile:

Thanks!

Ahhh Ok! Thank you

You’re most welcome!

Ok so this is weird… I did it exactly as you said and now its showing up, but it shows up about 5 times. http://2e6.274.myftpupload.com/category/real-weddings/

I made the font huge so you can see it easily. I checked the posts one and that still appears perfectly.

Hi Rena,

Ah, because it’s added for every integrity content. In that case, please change your entire code to this

add_action( 'x_before_view_global__index', 'print_breadcrumb_for_category' );
add_action( 'x_before_view_integrity_content', 'print_breadcrumb_for_post' );

function print_breadcrumb_for_category () {
if ( is_category() ) custom_breadcrumbs();
}
function print_breadcrumb_for_post () {
if ( is_singular( 'post' ) )  custom_breadcrumbs();
}
function custom_breadcrumbs() { ?>
		<div class="custom-breadcrumb">
			<a href="<?php echo get_permalink( get_option('page_for_posts') ); ?>">&#60; Return to Blog</a>
		</div>
		<?php
}

Different action while re-using the same breadcrumb. I also tested it and it works okay.

Thanks!

one last thing, promise… how can i add the search results page to that too?

Hi There,

Please find this code:

And change to this:

function print_breadcrumb_for_category () {
if ( is_category() || is_search() ) custom_breadcrumbs();
}

Let us know how it goes!

Perfect, thanks!

You’re welcome!
We’re glad our staff were able to help you out.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.