The Web-Feet blog uses h1 tags for the main title on each individual post, as you would commonly expect. However, from an SEO perspective, we did not want this same layout on the homepage. The reasoning behind this is that multiple h1 tags can instead carry a negative penalty. Therefore, we instead opted to display h2 tags in place of h1 tags on our blog’s homepage. One of the drawbacks of doing this meant that post titles on the homepage would look exactly the same as sub-titles within a post’s excerpt. From a user’s point of view, this isn’t ideal as it can be confusing and lead to a negative experience.
To work around this we had to delve into the dark depths of the WordPress core to find how and where post content was being pulled in. After much deliberation, we found the code we were looking for within the content.php file. The aim was to replace all h2 tags, apart from the post title, on the homepage with h3 tags. The code to do this wasn’t particularly complex in itself, providing you have some experience with regex, but the file structure of WordPress made something that should have been fairly simple into an afternoon’s work.
Much in contrast to this, content manipulation within Coaster CMS is extremely easy and can be done within page templates rather than requiring you to add a custom filter to a hook.
Code examples
This is the code we used with WordPress:
1 2 3 4 |
$content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); $content = preg_replace('/<h2>(.*)<\/h2>/', '<h3>$1</h3>', $content); echo $content; |
Something similar can be achieved with Coaster CMS using:
1 2 3 |
$content = preg_replace(‘/<h2>(.*)<\/h2>/’, ‘<h3>$1</h3>’, PageBuilder::block(‘content’)); {!! $content !!} |
As you can see the code example used with Coaster CMS is a lot easier to understand as opposed to that of WordPress, and doesn’t require you to set-up a filter. This code can also be inlined directly within templates.
External sources
RegExr – great for testing Regex patterns.