For example, if part of the content of the website only wants to be seen by the viewer, but does not want to be captured by the search crawler, the page content can be dynamically loaded through Ajax instead of being directly displayed in HTML. The hidden content can not be seen in the page source code. The search crawler will not normally recognize the content loaded by Ajax, and naturally can not obtain it. Here are the specific methods.
Add a JS file named my script in the theme directory js directory The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
/js/my-script.js
Add: The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
jQuery(document).ready(function($) { var data = { 'action': 'zm_action', 'data': 'zm_data' }; $.post(zm_script.ajax_url, data, function(response) { $('.zm-content').html(response); }); });
Add the following code to the theme function template functions.php file The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
//Load JS file function load_zm_script() { wp_enqueue_script( 'my-script', get_template_directory_uri() . '/ js/my-script.js', array('jquery'), '1.0', true ); wp_localize_script( 'my-script', 'zm_script', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )); } add_action( 'wp_enqueue_scripts', 'load_zm_script' );
Add the following code to the theme function template functions.php file The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
//Add ajax action add_action('wp_ajax_zm_action', 'zm_action_callback'); add_action('wp_ajax_nopriv_zm_action', 'zm_action_callback'); function zm_action_callback() { Echo 'The content you don't want to be crawled by the search crawler'; wp_die(); }
Modify the text in it. The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
Finally, add the The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
<div class="zm-content"></div>
Because the text content is loaded through Ajax, the search crawler cannot crawl it, and it can only be seen when the web page is opened normally. The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
The above Ajax action function only loads a paragraph of text, and you can also call WordPress articles, for example, calling 5 articles randomly: The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
//Add a random call to 5 articles Ajax action function zm_action_callback() { echo '<ul>'; $args = array( 'orderby' => 'rand', 'posts_per_page' => 5 ); $query = new WP_Query( $args ); while ( $query->have_posts() ) : $query->the_post(); echo '<li>'; echo '<a href="'; echo the_permalink(); echo '">'; echo the_title(); echo '</a>'; echo '</li>'; endwhile; wp_reset_postdata(); echo '</ul>'; wp_die(); }
The above random article calls will not be statically cached, and the articles will be displayed randomly every time they are refreshed. The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html
The article comes from the knowledge that more birds- https://zmingcx.com/prevent-web-content-from-crawlers.html Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!