# Problems with Breadcrumb on custom-post-type

**URL:** <https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730>\
**Category:** Pinegrow WP\
**Created:** [November 28, 2020, 9:16pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730 "2020-11-28T21:16:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [November 28, 2020, 9:16pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/1 "2020-11-28T21:16:38Z")

</div>

Hi experts

I have a WP project with pages and custom-post-types.

With the **structured pages** , the URL and breadcrumb display work perfectly:

![Auswahl_007](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/6/6da059c5938d7145af82733bdb22e29ed85a8035.png)

And here is a screenshot from my custom-post-type “news”: The URL works perfect, but the breadcrumb is incorrect. The slug of the custom-post-type “news” should appear there so that you have the link to the archive, but it does not appear.

![Auswahl_006](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/6/6cb7b31a3033a52058cc0cbc8f47b33d3a6b001d.png)

And when I’m in the archive-news.php the URL is correct, but the breadcrumb shows the title of the first post instead of the slug “news”.  
 ![Auswahl_005](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/a/a523dc67ff591906cdc1ceff4f5c0f0768158540.png)

Am I doing something wrong? Or does it not work (that would be very, very bad).?

Can someone help me please? Tank you.

Horst

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [November 30, 2020, 9:54pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/2 "2020-11-30T21:54:45Z")

</div>

Hi @HoWin,  
I’ll try to take a look at this tomorrow. Is there any other info you can give?  
Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 1, 2020, 8:12am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/3 "2020-12-01T08:12:30Z")

</div>

Hi Bob  
Thank you in advance 🙂

Here are some information about how I set it up:

**The breadcrumbs:**  
 ![Auswahl_355](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/5/5d06d6da3e7d525e12b5977a4a0a3b74886fd563.png)

**Pages** : Breadcrumbs work fine (pages are structured)  
 ![Auswahl_354](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/9/91e33120b77e55afb997c9326f7ae2494aae6748.png)

**Custom Post Type** : Breadcrumbs don’t work there (CPT are not structured)  
 ![Auswahl_353](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/f/f2d43622b69b6baff5550ab4a1b147da9e6933f7.png)

- For displaying the custom-post-types I use: single-{slug}.php and achrive-{slug}.php.
- For displaying the pages I use the template and linked posts (linked by a custom field).

Anything else you need?

I’m curious whether you can find the mistake or suggest a work-around.

Horst

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 1, 2020, 10:29am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/4 "2020-12-01T10:29:32Z")

</div>

Hi again,  
You set-up for the breadcrumbs looks okay.  
Can you show me how you registered the CPT?  
Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 1, 2020, 12:13pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/5 "2020-12-01T12:13:54Z")

</div>

Hi Bob

here are the screenshots:

 ![Auswahl_356](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/5/5ff1f6dd8890a978430d727ea0e933e4cae0efea.png)

 ![Auswahl_357](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/6/6c0f1236f4b570dddb64e99b38b27c372d21b1f9.png)

btw: I tried both settings at hierarchical, and I couldn’t see a difference.

Horst

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 1, 2020, 8:27pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/6 "2020-12-01T20:27:40Z")

</div>

Okay - confessing to learning as I go. As it turns out, the archive isn’t actually a “parent” of any of the CPT posts. So, to have it included in your breadcrumbs you will have to put it in programmatically.

One way would be by creating a single-companies.php template for displaying the companies CPT posts. On that page you could add a block of php for out-puting the breadcrumbs. You can still use the PG\_Helper::getBreadcrumbs to help you out.

Make sense?  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 1, 2020, 8:49pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/7 "2020-12-01T20:49:57Z")

</div>

Hi Bob  
I do have a single-companies.php and an archive-companies.php. Both templates use the same breadcrumps from index.php.

It should work this way, but it doesn’t.

Can it be that this is a bug?  
Horst

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 1, 2020, 8:53pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/8 "2020-12-01T20:53:38Z")

</div>

One thing is strange: The shown URL always is correct, only the breadcrumbs are wrong.

That’s why I think it could be a bug. What do you mean?

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 1, 2020, 9:36pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/9 "2020-12-01T21:36:21Z")

</div>

So, what is the hierarchy of the pages where it does work?

> [@HoWin](#):
>
> One thing is strange: The shown URL always is correct, only the breadcrumbs are wrong.

You mean that the URL in the browser bar is correct, but the breadcrumbs don’t show the part of the URL before the current post name?  
Also, can you just look at what your breadcrumbs look like if you have a grandchild CPT post?  
Thanks,  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 2, 2020, 7:02am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/10 "2020-12-02T07:02:23Z")

</div>

I tried to fix the problem with child-parent CPT, and the result is: correct breadcrumbs but wrong URL ☹

here is how it looks like with child-parent CPT:  
 ![Auswahl_358](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/5/5fc1e006016a7e743e9afd6716c6e3030d021e56.png)

and from the admin-panel:  
 ![Auswahl_359](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/f/f5e3a889f647acbc3a0f1a1869330ae6eb90491d.png)

On the other hand, child-parent PAGES are correct on both sides:  
 ![Auswahl_361](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/9/91aac85fa9f120f88ea2cc9900eb3500500ef3a0.png)

and here from the admin-panel:  
 ![Auswahl_360](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/7/7760889ed6114b05df5b18a33fbc896c7f61d8ad.png)

FYI: I also tried to change the breadcrumb-settings from “Post parents” to “Post categories”, but this also wouldn’t solve the problem with CPT. (I thought I can use two different breadcrumbs, depending on the page-type)

?? any idea ??  
Horst

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 2, 2020, 10:52am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/11 "2020-12-02T10:52:51Z")

</div>

I’m not quite sure what you changed. It looks the same to me.

I’m not surprised by the resulting behavior. If you look at the structure of your pages ‘Dienstleistungen’ is the parent that you get to from the home page. ‘Engineering’ is the child page you get to from ‘Dienstleistungen’. This is reflected in the breadcrumbs.

For your CPT, ‘Germany|Ober-Ramstadt’ is the parent you get to from the home page. ‘Child Company’ is the child page you get to from ‘Germany|Ober-Ramstadt’. This is also reflected in the breadcrumbs.

The breadcrumbs represent the path that you took through the website to get to where you are now. Since you did not have to navigate the archive, it isn’t included in the breadcrumbs.  
Under the hood, this uses the WP function, ‘get\_post\_ancestors’, which will not return the archive page. You would have to modify the PHP block if you want this behavior.

```auto
    <?php $breadcrumbs = PG_Helper::getBreadcrumbs( 'parents', true, 'Home'); ?>
    <?php if( !empty( $breadcrumbs ) ) : ?>
        <div cms-breadcrumbs-home cms-breadcrumbs-separator="span">
            <p><?php _e( 'Breadcrumb', 'st2' ); ?></p>
            <ul>
                <?php for( $breadcrumbs_i = 0; $breadcrumbs_i < count( $breadcrumbs ); $breadcrumbs_i++) : ?>
                    <?php $breadcrumb = $breadcrumbs[$breadcrumbs_i]; ?>
                    <li>
                        <a href="<?php echo esc_url( $breadcrumb['link'] ); ?>"><?php echo $breadcrumb['name']; ?></a>
                    </li>
//insert new code here
                    <?php if( $breadcrumbs_i < count( $breadcrumbs ) - 1 ) : ?>
                        <span>></span>
                    <?php endif; ?>
                <?php endfor; ?>
            </ul>
        </div>
    <?php endif; ?>

```

Here is the generated PHP when you use the WP Action in Pinegrow - yours may slightly differ based on your separator, plus I had a holder of ‘Breadcrumb’ that is in the code. The `$breadcrumbs` variable is populated with an array of all the post ancestors. I’ve indicated where to insert the code that I show below.

The easiest way I see to get the solution you want is to create a single-companies.php page for displaying the companies and to alter the PHP for the breadcrumbs on that page.

Let’s walk through the code a little. The first line populates the `$breadcrumbs` variable. Next, we set up an if loop to only display the breadcrumbs if they exist. Next is a `<div>` element for the breadcrumb output , followed by my placeholder in a `<p>`. Now we get to the main part.

The breadcrumbs are set up in an unordered list, `<ul>`. Nested in this is a for loop that goes through each of the elements of the $breadcrumbs array. Each element is outputted into a `<li>` element with a link(`<a>`) and name.

```auto
 <li>
     <a href="<?php echo esc_url( $breadcrumb['link'] ); ?>"><?php echo $breadcrumb['name']; ?></a>
 </li>

```

That is then followed by an if statement to insert your separator if there is another item in the breadcrumb array.

So, what you want to do is inject a link between the home breadcrumb and the first page breadcrumb. We can do this by inserting an if statement along with another divider above the existing if statement.

```auto
<?php if($breadcrumbs_i === 0) : ?>
    <span>></span>
    <li>
        <a href="<?php echo get_site_url() . '/companies/'; ?>"><?php echo 'Companies'; ?></a>
    </li>
<?php endif; ?>

```

So, in the second line this code will add your span after the ‘Home’ breadcrumb. You will need to change this to reflect your separator. It then adds a link for the archive, assuming you have pretty permalinks the allows access to the archive at ‘[https://your-site.com/companies](https://your-site.com/companies)’ .

Hope this helps,  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 2, 2020, 6:28pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/13 "2020-12-02T18:28:48Z")

</div>

Hi Bob

I see you gave me a workaround for my problem. I need to inject a link between home and first page. I’ll do this tomorrow and will inform you if this works.

But still, I think there is a bug in either WP or in PG. But if I can solve it (with your help) I’m happy with it.

I’ll keep you informed.

Horst

---

<div class="post-metadata">

**Author:** ![Emmanuel](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/emmanuel/32/6294_2.png) [@Emmanuel](https://forum.pinegrow.com/u/Emmanuel)\
**Post date:** [December 2, 2020, 7:22pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/14 "2020-12-02T19:22:48Z")

</div>

Edge cases.  
WP dev is full of possibilities, there are situations where smart actions are not enough and where hand coding special functions is mandatory. (or using code snippets from a third party)

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 3, 2020, 8:12am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/15 "2020-12-03T08:12:52Z")

</div>

Hi Emmanuel

Yes, I know that, and there was nothing I complained.  
The only reason for my question was: I wanted to know if I do something wrong or didn’t understand.

Many Thanks to @Bob, he helped me a lot.  
And many thanks to Pnegrow, it’s a phantastic app I use since many years.

Best regards,  
Horst

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 4, 2020, 6:15am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/16 "2020-12-04T06:15:53Z")

</div>

Hi @Bob

Thanks for your help. Your explanations helped me a lot.

For your information: I have now modified the whole thing so that it works equally for all single- {slug} .php and archive- {slug} .php.

I made the following modifications:

1.) First, I’ll define a couple of variables:

```
<?php 
	$Separator = "&nbsp;>&nbsp;";
    $p_Url = "/".get_post_type( get_the_ID() )."/";
    $p_Name = get_post_type_object( get_post_type( get_the_ID() ) )->labels->name;
?>

```

2.) This snippet of code goes to the place you suggested:

```
<?php if($breadcrumbs_i === 0) : ?>
    <span><?php echo $Separator; ?></span>
    <li>
        <a href="<?php echo $p_Url; ?>"><?php echo $p_Name; ?></a>
    </li>
    <?php if(is_archive()) $breadcrumbs_i = count( $breadcrumbs ); ?>
<?php endif; ?>

```

The whole thing now works great on all my CPT’s.

Important: with the CPT’s I now use the breadcrump definition above. For the standard posts and pages I use the standard breadcrumbs generated by the Pinegrow Smart-Action.

Thanks a lot Bob.

Kind regards,  
Horst

---

<div class="post-metadata">

**Author:** ![Emmanuel](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/emmanuel/32/6294_2.png) [@Emmanuel](https://forum.pinegrow.com/u/Emmanuel)\
**Post date:** [December 4, 2020, 7:09am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/17 "2020-12-04T07:09:43Z")

</div>

Thanks a lot to you for sharing your experience 🙂

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 4, 2020, 11:11am UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/18 "2020-12-04T11:11:42Z")

</div>

Hi Horst,  
Glad it is working and you found a custom solution. Just out of curiosity, why the second if statement? Doesn’t it throw an error without it’s own `endif;`?  
Bob

---

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [December 4, 2020, 1:56pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/19 "2020-12-04T13:56:06Z")

</div>

Hi Bob

The second if-statement is necessary for the archive-{slug}.php. This ends the breadcrumbs output. Otherwise the breadcrumb also shows the first entry of the displayed posts.

And no, this if-statement does not return an error message because that is the short form of an if-statement (one liner), and it doesn’t need an endif.

Horst

PS: and here how it looks now (compare it to my first screenshots) 🙂

Just for information: The CPT is called “news”, but it’s german name is “Aktuelles”. That’s why the difference in URL and breadcrumb. If my boss doesn’t accept this I have to rename the CPT. But the main thing is that the breadcrumb now shows the same behavior on all CPT’s, posts and pages 😉

The **archive-news.php** :

![Auswahl_366](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/3/345050ad3f324f720c7801e6963781fcbae896fc.png)

and here the **single-news.php** :  
 ![Auswahl_367](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/5/5fd4046392d39518b03288a7cdbe48607264dee0.png)

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 4, 2020, 6:52pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/20 "2020-12-04T18:52:21Z")

</div>

I knew you could leave off the endif for ternaries, didn’t realize you could also do it for straight if statements.  
Thanks!  
Bob

---

<div class="post-metadata">

**Author:** ![joshuadollar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/joshuadollar/32/3141_2.png) [@joshuadollar](https://forum.pinegrow.com/u/joshuadollar)\
**Post date:** [January 6, 2021, 1:21pm UTC](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730/21 "2021-01-06T13:21:07Z")

</div>

I have also had a breadcrumb issue with my website MyAssignmentHelp, please help me if any one knows this.

[Next page](https://forum.pinegrow.com/t/problems-with-breadcrumb-on-custom-post-type/4730.md?page=2)
