Find out how you can conditionally display the EDD cart in the navigation block or menu using code.
The navigation block is responsible for displaying the menu in WordPress block themes (or classic themes that have been given support). It allows to display a list of items, which are nested blocks, and it has improved a lot in the last updates.
Although it is quite customizable from the user interface, it does not allow you to show or hide items conditionally. Fortunately, WordPress 6.1 version introduces a PHP filter that allows you to interact with the navigation block.
This opens up a huge field of possibilities, such as adding the Easy Digital Downloads cart conditionally.
Let’s see how:
Snippet to add EDD cart to the menu conditionally
Make sure you are using a block theme (for example Twenty Twenty-Three) or if you are using a classic theme that you have given support to template parts and have a defined header.
Now, add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this code you use the filter block_core_navigation_render_inner_blocks
to add a button at the end of the menu when the user is on an Easy Digital Downloads download tab ( is_singular('download')
).
Or, in the second example, if you want to always display it except on the blog page.
The idea is that you use the conditional tag that works for you in each case (for example, is_front_page
, is_page
…).
You can see that having a filter to modify the navigation block gives you extra flexibility to what the user interface offers.
However, keep in mind that when using a PHP filter you have the limitation that you will only see the result in the web frontend, in the backend the changes will not be reflected.
Conclusions
As you can see, thanks to the new WordPress navigation block filter you can add items to the menu of any theme conditionally.
In this case you add the Easy Digital Downloads cart, but the possibilities are immense. In future tutorials you will see more ways to take advantage of it.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉