WordPress站点在后台媒体库或在编辑文章/页面的时候直接上传图片会为这个图片创建一个页面,它被称为附件页面。
这个附件页面生成是毫无用处的,并且百度还会收录这个附件页面,这可能会对我们站点的搜索排名产生负面的 SEO 影响,因为搜索引擎会将几乎没有文本的页面视为低质量内容。
虽然我们可以通过其他途径,如 robots.txt 禁止搜索引擎收录类似的附件页面,但是也不能保证所有搜索引擎都遵守。
所以最好的方法就是直接禁用我们网站上的这个附件页面。
下面这段代码函数可以完美解决禁止生成附件(图片)页面。
将下面代码functions.php 文件中即可:
/* * WordPress如何禁止生成附件(图片)页面 * 老熊猫整理 www<span class="xiomao-420-mypl" data-mark="seo"></span>.nuandao.cn */ function wpb_redirect_attachme<span class="xiomao-465-mypl" data-mark="seo"></span>nt_<span class="xiomao-468-mypl" data-mark="seo"></span>to_post() { if ( i<span class="xiomao-486-mypl" data-mark="seo"></span>s_attachment() ) { global $post; if( empty( $post ) ) $post = get_queried_object(); if ($post->post_parent) { $link = get_permalink( $post<span class="xiomao-627-mypl" data-mark="seo"></span>->post_parent ); wp_redirect( $link, '301' ); exit(); } else { // What<span class="xiomao-702-mypl" data-mark="seo"></span> t<span class="xiomao-704-mypl" data-mark="seo"></span>o do if parent p<span class="xiomao-720-mypl" data-mark="seo"></span>ost is not available wp_redirect( home_url(), '301' ); exit(); } } } add_actio<span class="xiomao-800-mypl" data-mark="seo"></span>n( 'template_redirect', 'wpb_redirect_attachment_to_pos<span class="xiomao-858-mypl" data-mark="seo"></span>t' );