So to be clear, I add another 'custom field of type ‘Switch’ to my CMS Collection, name it ‘Hidden’, and then paste the code you’ve provided into the ‘custom code’ ‘inside tag’ of the that CMS Collection’s Page Settings (see image to be sure)? And this will then allow me to determine (by toggling the Switch) which individual CMS Collection page I do and don’t want to be indexed by Search Engines?
I just had a situation where I combined three categories of posts: blog posts, news coverage, and press releases. This was helpful so I could put them all together in a grid (without 3rd party tools) and so I could use the same page template for them all.
Turned out our client always wanted the press releases and news coverage ones to link externally and to not be indexed.
So, I used a combo of Stephen’s idea + the ideas from that Google article and added a noindex, nofollow meta tag and also redirected to the homepage if the category was not blog posts. Here’s my code:
<script>
// excluding press releases and news coverage from being indexed for SEO
let category = '{{THIS IS THE CATEGORY SLUG FROM WEBFLOW CMS}}';
if (category != 'blog-post') {
var metaRobots = document.createElement('meta');
metaRobots.setAttribute('name', 'robots');
document.head.appendChild(metaRobots);
metaRobots.setAttribute('content', 'noindex, nofollow'); // tell robots no
window.location.href = '/'; // redirect to home page
}
</script>
Here is the solution, it was tested on a live webiste. You just need to replace: /your-collection-page-url/ with your actual URL, and thre you have it, you can set any single collection page to noindex. Also, this code should be added in head of Collection Page Template:
<script>
// Check if the current page URL matches the single collection page URL
if (window.location.pathname === '/your-collection-page-url/') {
// Find the existing meta tag with the name attribute set to 'robots'
var metaTag = document.querySelector('meta[name="robots"]');
// Check if the meta tag exists
if (metaTag) {
// Update the content attribute to 'noindex'
metaTag.setAttribute('content', 'noindex');
} else {
// Create a new meta tag with the name and content attributes set to 'robots' and 'noindex' respectively
metaTag = document.createElement('meta');
metaTag.setAttribute('name', 'robots');
metaTag.setAttribute('content', 'noindex');
document.head.appendChild(metaTag);
}
}
</script>