I came across weird behaviour recently relating to breadcrumbs. In a site I was working on the breadcrumb functionality (provided by a plugin) was not consistent – it was incorrect on single post pages but correct on category pages. It didn’t make sense. There are not many options for the breadcrumb plugin so it was difficult to see the fault lying there. I did notice that the site used a custom menu and that code was used on all pages. So, my troubleshooting took to dissecting the header and then menu code.

In the header was a call to the custom menu and in that file each menu element was created with a new WP_Query to get a subset of links / pages. I saw that the syntax to fire off each query was as I would expect  but I did notice that the $wp_query object wasn’t saved before these additional queries and subsequently restored afterwards. So, I figured that was messing things up and added that code in. No change. Now, I went a little deeper and added code in the menu to display the current categories after each menu (the categories are used in the breadcrumbs). Now, it was making sense. So, the categories were being messed up after each query in the menu code and that affected the single post pages.

The fix was simple and one line of code at the end of the custom menu code before returning to finish the header code:

wp_reset_query();

And, as per the codex for wp_reset_query() it is recommended to be used after a custom query. The breadcrumbs were now working as expected.


0 Comments

Leave a Reply

Avatar placeholder