下面有重定向附件的代码,但对于音频或视频等特定媒体类型又如何呢?
function sar_attachment_redirect() {
global $post;
if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) {
wp_redirect(get_permalink($post->post_parent), 301); // permanent redirect to post/page where image or document was uploaded
exit;
} elseif ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent < 1) ) { // for some reason it doesnt works checking for 0, so checking lower than 1 instead...
wp_redirect(get_bloginfo(\'wpurl\'), 302); // temp redirect to home for image or document not associated to any post/page
exit;
}
}
add_action(\'template_redirect\', \'sar_attachment_redirect\',1);
SO网友:Majid
您必须使用此功能wp_check_filetype
检查您的媒体类型
$filetype = wp_check_filetype(\'image.jpg\');
echo $filetype[\'ext\']; // will output jpg
那么你可以吃这样的东西
$file_url = wp_get_attachment_url( $file_id );
$filetype = wp_check_filetype( $file_url );
switch ($filetype) {
case \'image/jpeg\':
case \'image/png\':
case \'image/gif\':
return // do whatever you want
break;
case \'video/mpeg\':
case \'video/mp4\':
case \'video/quicktime\':
return // do whatever you want
break;
case \'text/csv\':
case \'text/plain\':
case \'text/xml\':
return // do whatever you want
break;
default:
return // do whatever you want
}
因此,在您的函数中,您可以首先检查附件文件类型,当它是视频时,您可以执行重定向代码。
有关更多信息:
1-https://codex.wordpress.org/Function_Reference/get_post_mime_type
2-https://codex.wordpress.org/Function_Reference/wp_check_filetype
希望对你有用。