Skip Navigation

[Resolved] Multiple Sets of Taxonomical Breadcrumbs on Custom Post Type Template Page

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 10 replies, has 2 voices.

Last updated by graemeH-3 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2376593

I've done a few searches for this, and found some similar problems, but nothing that's helped me to figure out how exactly I should do this.

I have my custom post type, called "locations", and location post types will be in a couple different taxonomies, for example, /laundromats-near-me/ and in /tailors-near-me/.

Once someone reaches the custom post page, the individual location page, they will have arrived there in maybe one of a couple different ways as follows:

/laundromats-near-me/california/san-diego >> from here they find the business listing and can click on it,

or from

/tailors-near-me/california/san-diego >> from here they find the business listing and can click on it.

I want to add multiple (static) breadcrumbs to my location custom post page so that users can navigate back to the taxonomy archive page where they likely found the location post.

I would do this on the locations custom post type template page, using HTML to add the JSON+LD code as I learned from Waqar, and to add some HTML to show the same breadcrumb link structure.

In this example, a location is in 2 taxonomies, laundromats-near-me, and tailors-near-me, and within each of those it's in the sub taxonomies of california and san-diego.

To implement this I think I need the following:

- a way to only display breadcrumb for laundromats on the location page when it is in the laundromats taxonomy, likewise for the tailor taxonomy
- a way to call the taxonomy slug for each the /laundromats-near-me/ , /laundromats-near-me/california, and for /laundromats-near-me/california/san-diego
- a way to call the taxonomy title for each /laundromats-near-me/ , /laundromats-near-me/california, and for /laundromats-near-me/california/san-diego
- 2 custom HTML blocks on my template page, one which is the structure of breadcrumb JSON+LD code which I can just enter in my short codes for the taxonomy slugs and titles, and one for the HTML which will show the user the top of page breadcrumb list with links

From the reading I've done, I'll need to create a new oldschool View to insert on my location page template?

Any direction is appreciated ! 🙂

Thanks!

G

#2376905

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To achieve a custom breadcrumb structure for your hierarchal taxonomy terms, you can use a custom shortcode, which can return the taxonomy term links, for individual levels, separately.

You'll find a good example of a shortcode like this, in this forum reply:
https://toolset.com/forums/topic/display-different-parts-of-custom-taxonomy-separately/#post-1158659

Once the custom shortcode has been registered, you can use this shortcode to construct the breadcrumb for each taxonomy term and level like this:


// Taxonomy 1 Level 1 >> Taxonomy 1 Level 2 >> Taxonomy 1 Level 3
[get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-1" level="1"] >> [get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-1" level="2"] >> [get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-1" level="3"]

// Taxonomy 2 Level 1 >> Taxonomy 2 Level 2 >> Taxonomy 2 Level 3
[get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-2" level="1"] >> [get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-2" level="2"] >> [get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug-2" level="3"]

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2377411

This is great thanks Waqar.

So taking this code, I think I can add it in the Toolset>Settings>Custom Code section like previous code I have added instead of to the functions.php file ?
https://toolset.com/forums/topic/display-different-parts-of-custom-taxonomy-separately/#post-1158659

I don't think any of that code should conflict with any of my other custom codes which you've helped me with to display top level taxonomies and so forth...

To confirm, to add this code as above to get the different taxonomy slugs, this should be done in a old school view construction?

Or can it just be done in HTML/shortcode directly onto the custom post type (location) content template?

If I can simply enter these short code terms on the content template for the location to build my breadcrumb JSON+LD code and HTML links, how would you suggest that I make sure that for example, laundromat breadcrumbs only show up on the custom post location page when the location is in the laundromat category?

Would you think just using a simple conditional block with the HTML/shortcodes inside of it?
(I tried looking through the conditional block on my content template for the location pages and I couldn't see anyway to use it to check if a location was part of the laundromats or tailor taxonomies)

Maybe this is where using the old school view construction comes in to play.

Thanks again for your expertise!
G

#2378943

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

> So taking this code, I think I can add it in the Toolset>Settings>Custom Code section like previous code I have added instead of to the functions.php file ?
> I don't think any of that code should conflict with any of my other custom codes which you've helped me with to display top level taxonomies and so forth...

- Yes, your understanding is correct.

> To confirm, to add this code as above to get the different taxonomy slugs, this should be done in a old school view construction?
> Or can it just be done in HTML/shortcode directly onto the custom post type (location) content template?

- You don't have to recreate/convert your blocks based content template into a classic/legacy editor content template. The HTML and shortcodes for this breadcrumb can be added into a "Fields and Text" block in the existing content template.

> how would you suggest that I make sure that for example, laundromat breadcrumbs only show up on the custom post location page when the location is in the laundromat category?
> Would you think just using a simple conditional block with the HTML/shortcodes inside of it?

To conditionally show each set of breadcrumbs for each taxonomy, you can use the parent level shortcode in the conditional evaluation. The shortcode for it can be placed in the conditional block's advanced condition editor, like this:


NOT ( empty( '[get_terms_by_level id=[wpv-post-id] taxonomy=taxonomy-slug-1 level=1]') ) 

Note: Please, be sure to add the shortcode name "get_terms_by_level" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

#2379073

Waqar, this is great thank you.

I will test implement this.

I am curious on your last note,

>Note: Please, be sure to add the shortcode name "get_terms_by_level" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

Simply curious because previously, in one of my support tickets,
https://toolset.com/forums/topic/display-parent-taxonomy-title-with-link-on-custom-wordpress-child-archive-page/

I added this code to the Toolset>Setting>Custom Code section, to get the slug for a taxonomy, 'top-most-parent-slug', this was for doing breadcrumbs on taxonomy pages. Worked great.

In this instance, I didn't add the shortcode name to the "Third-party shortcode arguments" section like you suggest for the "get_terms_by_level".

Is this important? Did I miss a step?

The slug and other short code I'm using to get 'top-most-parent-title', all seem to be working...

Thanks again!

#2379339

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

It is generally a good practice that for any custom shortcode that we register on the website, we also add its name at WP Admin -> Toolset -> Settings -> Front-end Content.

This step allows Toolset to identify the custom shortcode when they're used in complex operations, for example in the conditional evaluations or when used as an attribute of another shortcode, e.g.


[shortcode_1 item="[shortcode_2]"]

When we simply have to output a custom shortcode directly, it is fine if it is not added at WP Admin -> Toolset -> Settings -> Front-end Content.

#2379953

Hi Waqar,

so this works great, for the on page, static breadcrumbs.

I guess where I'm tripped up is, where can I get the taxonomy titles, and not links, for the JSON+LD breadcrumb structure.

The term links for level 1 and 2 work great in my case for the links, but with the JSON+LD code I need to provide a name for those pages, which I don't think I can just throw a link in for.

Here's the example from Google Search Central,

<script type="application/ld+json">
{
"@context": "hidden link",
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"name": "Books",
"item": "hidden link"
},{
"@type": "ListItem",
"position": 2,
"name": "Science Fiction",
"item": "hidden link"
},{
"@type": "ListItem",
"position": 3,
"name": "Award Winners"
}]
}

So as example above, for the "item" field, I can use the shortcode,

[get_terms_by_level id=[wpv-post-id] taxonomy=taxonomy-slug-1 level=1]

But for the "name" field, I need just the name of the taxonomy level 1, not it's link.

Is there an easy way to pull out the taxonomy title for this section?

Thanks again! So far this is working great!

#2380267

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You can update that custom shortcode, so that it can return both, term title/name or term link, through a shortcode attribute 'return':


add_shortcode('get_terms_by_level', 'get_terms_by_level_func');
function get_terms_by_level_func($atts) {

	$taxonomy = $atts['taxonomy'];
	$postid = $atts['id'];
	$level = $atts['level'];
	$return = $atts['return'];

	if( !(isset($level)) || (empty($level)) ) {
		$level = 1;
	}

	$terms = get_the_terms( $postid, $taxonomy);

	if ( $terms && ! is_wp_error( $terms ) ) {
		foreach ($terms as $term) {
			if($term->parent == 0) {
				$results['1'] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);     
			} else {
				$tmpresults[] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);
			}
		}

		foreach ($tmpresults as $result) {
			if($result['parent'] == $results['1']['id']) {
				$results['2'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
			}
			else {
				$results['3'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
			}
		}

		if($return == 'title') {
			return $results[$level]['name'];
		} elseif ($return == 'link') {
			return '<a href="' . esc_url( get_term_link( $results[$level]['id'], $taxonomy) ) . '">' . $results[$level]['name'] . '</a>';
		} 
	}

}

After this change, you can include the attribute return="link" for the term link:


[get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug" level="1" return="link"] 

And the attribute return="title" for the term title/name:


[get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug" level="1" return="title"] 

#2380653

Thanks Waqar, this works great!

I did realize though, that the return "link", was the ahref, and in my case, for the JSON+LD code, when you put in the "item" it should be just a URL, not an ahref.

I added another elseif at the bottom, just removing the ahref code that you had there, and this seems to work.

I'll paste the code here just in case it helps someone, and, could you please have a look and let me know if there is any issue you can see from your experience in doing it like this?

Now, to return just the URL that I need for the "item" section of my JSON+LD code I just use,

[get_terms_by_level id="[wpv-post-id]" taxonomy="taxonomy-slug" level="1" return="url"] 
add_shortcode('get_terms_by_level', 'get_terms_by_level_func');
function get_terms_by_level_func($atts) {
 
    $taxonomy = $atts['taxonomy'];
    $postid = $atts['id'];
    $level = $atts['level'];
    $return = $atts['return'];
 
    if( !(isset($level)) || (empty($level)) ) {
        $level = 1;
    }
 
    $terms = get_the_terms( $postid, $taxonomy);
 
    if ( $terms && ! is_wp_error( $terms ) ) {
        foreach ($terms as $term) {
            if($term->parent == 0) {
                $results['1'] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);     
            } else {
                $tmpresults[] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);
            }
        }
 
        foreach ($tmpresults as $result) {
            if($result['parent'] == $results['1']['id']) {
                $results['2'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
            }
            else {
                $results['3'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
            }
        }
 
        if($return == 'title') {
            return $results[$level]['name'];
        } elseif ($return == 'link') {
            return '<a href="' . esc_url( get_term_link( $results[$level]['id'], $taxonomy) ) . '">' . $results[$level]['name'] . '</a>';
        } 
      elseif ($return == 'url') {
            return esc_url( get_term_link( $results[$level]['id'], $taxonomy) );
        } 
    }
 
}

Thanks again for the help, I think this time, this is actually the last step of the directory I'm trying to build, which is amazing and I really appreciate all the help along the way from you and the rest of the support staff.

Please let me know if you see anything wrong with my added elseif to return just the URL from your experience 🙂

Thanks!
G

#2380933

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your kind words and so glad that we were able to assist you with this progress.

The code looks and works great and the "url" output is incorporated, correctly.

You're welcome to mark this ticket as resolved and start a new one for each new question or concern. We're here to help!

#2381693

Thanks Waqar, appreciate your proof reading "my work" after I copy it from your code 😛

Thanks again!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.