Create Custom Breadcrumb Links for The7 Theme Posts
Using WordPress pages to display blog posts or articles instead of using the category archives can pose serious problems with breadcrumb navigation. Plus it’s just a bad user experience if the website has visible duplicate content that causes visitors to get lost. This tutorial will show you how to create custom breadcrumb links for posts when using The7 theme.
Blog Page Template vs Category Archive
When you publish a post in WordPress, a category archive is also created that includes the new post. Even if a category is not selected, the post will still show in the Uncategorized category. Unfortunately, the category archives are turnkey and created by WordPress, and editing options are minimal unless you know how to develop in PHP.
To solve this problem, The7 theme includes a Blog template for pages to allow extra layout options for a better user experience. Keep in mind, though, that creating a separate page to display posts will create duplicate content, and the internal links will still resolve back to the category archives.
For example, if a visitor is viewing a page that has posts on it, clicks on the post to read it, and then clicks on the category link within the post’s content, the visitor will be redirected back to the category archive rather than the previous page.
Another problem is that the breadcrumbs in the post will also redirect to the category archive rather than the previous page. The7 theme does not offer a solution to rewrite URLs for breadcrumbs, and the end result is the visitor may end up looking at a category archive page if trying to navigate the website.
Here is a simple solution to create custom breadcrumb links and rewrite the category archive URLs for posts so that the internal links resolve to the blog page template instead of the category archive.
Step 1: Replace the Breadcrumb Category Archive Link with the Page Link
The first part of this solution is to replace the link that is displayed in the breadcrumb. The7 theme uses a filter for this called presscore_get_breadcrumbs.
Paste the following code example into the theme or child theme functions.php file:
The code snippet uses the PHP function str_replace to find the occurrence of a string and replace it. The entire link is stored in $args. Make sure to edit the code to match the URL structure on the website.
The code example expects that you have created post categories named “News” and “Blog”, and have also created pages with the same name and slug.
Step 2: Replace the Category Archive URL with the Page URL
The next step is to rewrite the URLs so that they resolve correctly. This code example is similar to the one above, but it uses a separate filter called presscore_posted_on_parts.
Again, paste the following code example into the theme or child theme functions.php file:
Edit the code snippet accordingly to match the URL structure of the website.
If the code is going to be used for another post type, like the portfolio, change line 8 so that it checks whether the post type is a custom post type registered by The7 theme.
Portfolio
get_post_type( get_the_ID() ) == 'dt_portfolio'
Testimonials
get_post_type( get_the_ID() ) == 'dt_testimonials'
Team
get_post_type( get_the_ID() ) == 'dt_team'
Partners, Clients, etc.
get_post_type( get_the_ID() ) == 'dt_logos'
Benefits
get_post_type( get_the_ID() ) == 'dt_benefits'
Photo Albums
get_post_type( get_the_ID() ) == 'dt_gallery'
Slideshows
get_post_type( get_the_ID() ) == 'dt_slideshow'
Final Thoughts: Create Custom Breadcrumb Links for The7 Theme Posts
This solution for displaying the edited breadcrumbs and rewriting the category archive URLs requires both of the code examples above.
When using pages to display post archives instead of using the category archives, it would be a good idea to set the category archives to NO INDEX so that duplicate content is not added to the search engine results. Use a plugin like Yoast SEO to manage this type of feature.
For more information about The7 theme:
Need help? Our WordPress Development Service is a comprehensive solution for small, medium, and enterprise-level businesses. Call us at 602-633-4758 for a free consultation.
Great resource. I’m using a custom post type and want the “parent” to link to specific page and not the archive.
For example “Home / Services / Single Item”
One the “Single Item” page the “Service” links to the current post. I can’t seem to get it to work.
Hi Eric, if you are using The7 theme and also using the code snippets in this article, you will need to modify line 8 (both code snippets) to reference your custom post type. For example: if( get_post_type( get_the_ID() ) == ‘custom_post_type_name’ ) {
Here is an article on this website that will help you discover the name of the custom post type: https://www.goldenoakwebdesign.com/blog/how-to-add-the7-theme-sidebar-to-custom-post-types-in-wordpress