Is it possible to not index part of a page, Elementor
-
I have a site where at the bottom of pages we show the latest posts via an Elementor widget. Is it possible to exclude these from the search results as they produce false positives for searches?
For example, a search for ‘car’ would show a result for the Terms and Conditions page as the posts in the latest news widget at the end of that page might have the word car in the title, but noting within the page content did.
-
Yes, it’s possible. See Avoid indexing menus instructions. Those instructions are for Divi, but it’s similar for Elementor: figure out the shortcode Elementor uses to add the latest posts on the page and use that filter hook to remove the shortcode before Relevanssi indexes the page.
If you can figure out what the shortcode is, I can help to come up with the necessary code.
Thanks. I’ve reached out to Elementor on Git Hub and asked for the information. I will let you know what they come back with.
You can see the shortcodes in the post source code in WordPress, that’s the fastest way to check it. Look at what the element looks like in the WP post code editor view.
Elementor doesn’t place shortcodes in the tinymce editor
Oops, sorry, I always mix it up with Divi, which uses shortcodes.
Which element are you using to display the latest posts? I tried adding a Posts element to an Elementor page and Relevanssi just ignored it, looks like the element doesn’t appear in the post content at all.
If you’re using the Gutenberg Latests Posts block in the post content, Relevanssi does index that. The easiest way to make Relevanssi skip any blocks from Gutenberg is to give the block the CSS class
relevanssi_noindex
.Ah I see ??
No, it’s in Elementor. Using the posts widget from the Ultimate Addons for Elementor plugin https://ultimateelementor.com/widgets/posts/
I will give the element a class of
relevanssi_noindex
and see if that works.No That didn’t do anything. The class is there, but it is still indexed.
see https://weeatbalanced.com/?s=pig And ‘About the experts’ comes up, when the area of the page that contains the term ‘pig’ is in a section with the
relevanssi_noindex
class on it.Are you using Gutenberg? The
relevanssi_noindex
class only works for Gutenberg blocks.I don’t have Ultimate Addons for Elementor, so I can’t check that myself. What if you do this:
add_filter( 'relevanssi_post_content', function( $c ) { error_log( $c ); return $c; } );
Then go save the post. This should print out the post content as Relevanssi sees it in the server error log. How does that element appear there?
I added it to the functions.php file, but it didn’t do anything.
Correction, it is working. However, it outputs HTML without any wrapping div’s like this.
<h4>Latest articles</h4> <a role="button" href="/all-articles/"> View all articles </a> <a title="Meeting sustainability targets: UK meat industry working together" href="https://weeatbalanced.com/sustainability/uk-meat-industry-working-together-to-meet-sustainability-targets/" target="_self" rel="noopener"><img src="/wp-content/uploads/2022/02/dreamstime_l_63217286-1024x576.webp" alt="A bin overflowing with food" width="800" height="450" /></a> Sustainability <h3><a href="https://weeatbalanced.com/sustainability/uk-meat-industry-working-together-to-meet-sustainability-targets/" target="_self" rel="noopener"> Meeting sustainability targets: UK meat industry working together </a></h3> <a title="Can we decrease the environmental impact of eating meat?" href="https://weeatbalanced.com/sustainability/can-we-decrease-the-environmental-impact-of-eating-meat/" target="_self" rel="noopener"><img src="/wp-content/uploads/2021/11/Can-we-decrease-the-environmental-impact-of-eating-meat-1-1024x576.png" alt="Can we decrease the environmental impact of eating meat" width="800" height="450" /></a> Sustainability <h3><a href="https://weeatbalanced.com/sustainability/can-we-decrease-the-environmental-impact-of-eating-meat/" target="_self" rel="noopener"> Can we decrease the environmental impact of eating meat? </a></h3> <a title="Should we cut out dairy products from our diet?" href="https://weeatbalanced.com/health-and-nutrition/should-we-cut-out-dairy-products-from-our-diet/" target="_self" rel="noopener"><img src="/wp-content/uploads/2021/04/Should-we-cut-out-dairy-products-from-our-diet-optimised-1024x576.jpeg" alt="" width="800" height="450" /></a> Health & Nutrition <h3><a href="https://weeatbalanced.com/health-and-nutrition/should-we-cut-out-dairy-products-from-our-diet/" target="_self" rel="noopener"> Should we cut out dairy products from our diet? </a></h3> <a title="Why are whole grains good for your health?" href="https://weeatbalanced.com/health-and-nutrition/benefits-of-wholegrains/" target="_self" rel="noopener"><img src="/wp-content/uploads/2021/04/Why-are-whole-grains-good-for-your-health-optimised-1024x576.jpeg" alt="" width="800" height="450" /></a> Health & Nutrition <h3><a href="https://weeatbalanced.com/health-and-nutrition/benefits-of-wholegrains/" target="_self" rel="noopener"> Why are whole grains good for your health? </a></h3> <a title="Can eating potatoes increase risk of type 2 diabetes?" href="https://weeatbalanced.com/health-and-nutrition/can-eating-potatoes-increase-risk-of-type-2-diabetes/" target="_self" rel="noopener"><img src="/wp-content/uploads/2021/03/maris-piper-1280x720-1-1024x576.jpg" alt="A top down view of a basket of washed Maris Piper potatoes." width="800" height="450" /></a> Health & Nutrition <h3><a href="https://weeatbalanced.com/health-and-nutrition/can-eating-potatoes-increase-risk-of-type-2-diabetes/" target="_self" rel="noopener"> Can eating potatoes increase risk of type 2 diabetes? </a></h3> <a title="The benefits of eating dairy for children and adults" href="https://weeatbalanced.com/health-and-nutrition/dairy-health-benefits-through-the-life-stages/" target="_self" rel="noopener"><img src="/wp-content/uploads/2020/12/The-benefits-of-eating-dairy-for-children-and-adults-1024x576.png" alt="The benefits of eating dairy for children and adults" width="800" height="450" /></a> Health & Nutrition <h3><a href="https://weeatbalanced.com/health-and-nutrition/dairy-health-benefits-through-the-life-stages/" target="_self" rel="noopener"> The benefits of eating dairy for children and adults </a></h3> <a title="Nutrients in beef, lamb and pork" href="https://weeatbalanced.com/health-and-nutrition/red-meat-nutritional-benefits/" target="_self" rel="noopener"><img src="/wp-content/uploads/2020/12/Nutrients-in-beef-lamb-and-pork-1024x576.png" alt="Nutrients in beef, lamb and pork" width="800" height="450" /></a> Health & Nutrition <h3><a href="https://weeatbalanced.com/health-and-nutrition/red-meat-nutritional-benefits/" target="_self" rel="noopener"> Nutrients in beef, lamb and pork </a></h3> <a title="Can you tell us about British pig farming?" href="https://weeatbalanced.com/food-and-farming/british-pig-farming/" target="_self" rel="noopener"><img src="/wp-content/uploads/2020/12/Can-you-tell-us-about-British-pig-farming-1024x576.png" alt="A group of pigs in a field" width="800" height="450" /></a> Food & Farming Standards <h3><a href="https://weeatbalanced.com/food-and-farming/british-pig-farming/" target="_self" rel="noopener"> Can you tell us about British pig farming? </a></h3> <a title="Can you tell us about British dairy farming?" href="https://weeatbalanced.com/food-and-farming/british-dairy-farming/" target="_self" rel="noopener"><img src="/wp-content/uploads/2020/12/Can-you-tell-us-about-British-dairy-farming-1024x576.png" alt="Can you tell us about British dairy farming" width="800" height="450" /></a> Food & Farming Standards <h3><a href="https://weeatbalanced.com/food-and-farming/british-dairy-farming/" target="_self" rel="noopener"> Can you tell us about British dairy farming? </a></h3>
You saved the post, it’s an indexed post and Relevanssi has indexed post content for that post? Just saving the post in Elementor isn’t necessarily enough to trigger Relevanssi indexing, but quicksaving it in the WP admin should be.
If the filter hook isn’t firing at all, then the post shouldn’t be indexed by Relevanssi at all.
Looks like Elementor adds the wrapping divs later, then. That makes it somewhat trickier to remove, but if it’s the last element on the page, you can just remove anything after the heading. This should remove the “Latest articles” element from the Relevanssi index:
add_filter( 'relevanssi_post_content', function( $content ) { $content_parts = explode( '<h4>Latest articles</h4>', $content ); return $content_parts[0]; } );
Add this and rebuild the index.
- This reply was modified 2 years, 8 months ago by Mikko Saari.
Hmm that works for that one page. Other pages use that element with a different title, but if I change the snippet to match those it doesn’t work??
I tried something else too. I thought perhaps if I was to add a HTML comment and then amend your code like this …
add_filter( 'relevanssi_post_content', function( $content ) { $content_parts = explode( '<!-- relevanssi_noindex -->', $content ); return $content_parts[0]; } );
but that didn’t work either ??
Multiple headings are not a problem.
add_filter( 'relevanssi_post_content', function( $content ) { $content_parts = explode( '<h4>Latest articles</h4>', $content ); if ( 1 === count( $content_parts ) ) { $content_parts = explode( '<h4>Another heading</h4>', $content ); } if ( 1 === count( $content_parts ) ) { $content_parts = explode( '<h4>Yet another heading</h4>', $content ); } if ( 1 === count( $content_parts ) ) { $content_parts = explode( '<h4>These headings go on forever</h4>', $content ); } return $content_parts[0]; } );
etc., you can add multiple headings there. The function will keep on trying new ones until it finds one that splits the post into at least two parts.
Sorry for the late reply but that worked great!
Thanks
- This reply was modified 2 years, 8 months ago by Ash.
- The topic ‘Is it possible to not index part of a page, Elementor’ is closed to new replies.