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.
get_post_type( get_the_ID() ) == 'dt_portfolio'
get_post_type( get_the_ID() ) == 'dt_testimonials'
get_post_type( get_the_ID() ) == 'dt_team'
Partners, Clients, etc.
get_post_type( get_the_ID() ) == 'dt_logos'
get_post_type( get_the_ID() ) == 'dt_benefits'
get_post_type( get_the_ID() ) == 'dt_gallery'
get_post_type( get_the_ID() ) == 'dt_slideshow'
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: