問題描述
當我們在 Google 上搜索我們博客上的內容時,我注意到我的震撼和恐懼,媒體 Library 的個人圖像以某種方式生成自己的 URL,Google 以某種方式找到並編制索引!
例如這個頁面:http://blog.stackoverflow.com/2008/08/special-development-team-podcast/
包含圖片:http://blog.stackoverflow.com/wp-content/uploads/bio-jarrod-dixon.jpg
這是很好的,但不知何故,這個圖像也暴露為自己的 URL 和”post”:http://blog.stackoverflow.com/2008/08/special-development-team-podcast/bio-jarrod-dixon/
這是非常不需要的!
我檢查了 WordPress 中的媒體設置,並瀏覽了媒體庫,但我無法找出一種方法來禁用此行為。有任何想法嗎?
最佳解決方案
你所説的這個東西是不需要的只是 WordPress 中的正常功能,它不能被刪除。但是,有些事情您可以將不需要的 URL 指向更有用的東西。
這是一個關於這個問題的論壇帖子,有一些有趣的修復和關於發生的事情的描述:
http://wordpress.org/support/topic/disable-attachment-posts-without-remove-the-medias
Attachments are actually a post type, so they take a row in the posts table like a post does, they will always have a URL available, in the same way that posts do to..
ie.
example.com/?p=16
16 is the post ID, and like posts they will always be available by a URL like the above. Media files aren’t simply considered files, they have a more content like element to them, in that they have a record in the posts table that corresponds to them, just like a post or page does.
What you’re asking is how to stop the automatic existance of individual attachment URLs for each media item(not really possible because they’re essentially a post type, which means they’ll always be a URL for them).
Here’s a suggestion though, take any template(theme) file, index.php, page.php, archive.php or whatever you like, create a copy and rename it to image.php or attachment.php if you want to target all media. Open the file, remove the loop, save… and load up one of the attachment pages(like the one you provided before)..
My point being, all you need to do is create an attachment template file: http://codex.wordpress.org/Template_Hierarchy
http://codex.wordpress.org/Template_Hierarchy#Attachment_displayIf you wanted to, in theory you could place a redirect into the attachment template so individual attachment views are redirected (or any number of other things you might want to do).
有人發佈了一個 attachment.php
,可以在您的/themes
文件夾中重定向:
<?php
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.get_permalink($post->post_parent));
?>
次佳解決方案
我想到這是關於時間,我至少嘗試我的手擦除附件頁面。
這是我的第一槍
add_filter( 'attachment_fields_to_edit', 'wpse_25144_attachment_fields_to_edit', 10000, 2 );
function wpse_25144_attachment_fields_to_edit( $form_fields, $post ) {
$url_type = get_option( 'image_default_link_type' );
if( 'post' == $url_type ) {
update_option( 'image_default_link_type', 'file' );
$url_type = 'file';
}
$form_fields['url'] = array(
'label' => __('Link URL'),
'input' => 'html',
'html' => wpse_25144_image_link_input_fields( $post, $url_type ),
'helps' => __('Enter a link URL or click above for presets.')
);
return $form_fields;
}
function wpse_25144_image_link_input_fields($post, $url_type = '') {
$file = wp_get_attachment_url($post->ID);
if( empty( $url_type ) )
$url_type = get_user_setting( 'urlbutton', 'file' );
$url = '';
if( $url_type == 'file' )
$url = $file;
return "
<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
<button type='button' class='button urlnone' title=''>" . __('None') . "</button>
<button type='button' class='button urlfile' title='" . esc_attr($file) . "'>" . __('File URL') . "</button>
";
}
add_filter( 'query_vars', 'wpse_25144_query_vars', 10000, 2 );
function wpse_25144_query_vars( $wp_query_vars ) {
foreach( $wp_query_vars as $i => $qv ) {
if( in_array( $qv, array( 'attachment', 'attachment_id' ) ) )
unset( $wp_query_vars[$i] );
}
return $wp_query_vars;
}
add_filter( 'attachment_link', 'wpse_25144_attachment_link', 10000, 2 );
function wpse_25144_attachment_link( $link, $id ) {
$link = wp_get_attachment_url( $id );
return $link;
}
add_filter( 'rewrite_rules_array', 'wpse_25144_rewrite_rules_array', 10000 );
function wpse_25144_rewrite_rules_array( $rewriteRules ) {
foreach( $rewriteRules as $pattern => $query_string ) {
if( false === strpos( $pattern, 'attachment' ) && false === strpos( $query_string, 'attachment' ) )
continue;
unset( $rewriteRules[$pattern] );
}
return $rewriteRules;
}
刪除附件重寫,更新附件鏈接指向附件文件 (而不是固定鏈接),刪除附件查詢變量,並刪除將附件鏈接到現在的 non-existant 附件永久鏈接的功能。
開放批評:)
第三種解決方案
您可以對其父頁面的附件進行 301 重定向,如下所示:
<?php
/*
Plugin Name: Redirect Attachments to Parent (301)
Plugin URI: http://wordpress.stackexchange.com/questions/25144/unwanted-media-library-urls-in-posts
Description: Redirect any attachemnt pages to their parent's page with 301 redirection
Author: Ashfame
Version: 0.1
Author URI: http://www.ashfame.com/
*/
add_action( 'template_redirect', 'attachment_post_type_redirection' );
function attachment_post_type_redirection() {
global $wp_query;
if ( is_attachment() ) {
wp_redirect( get_permalink( $wp_query->post->post_parent ), 301 );
}
}
第四種方案
Yoast’s SEO plugin 在 「永久鏈接」 下有 「將附件 URL 重定向到父帖子」 。我使用此選項來解決問題。插件是驚人的
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。