添付ファイルページではなくファイル(例:PDF)へのリンク
WordPressでは、デフォルトで、検索結果として返されたメディアの添付ファイルページにリンクします。多くのテーマではメディア用の便利なテンプレートがなく、ファイル自体に直接リンクする方が理にかなっていることがよくあります。
このスニペットは、検索結果テンプレートをカスタマイズして、添付ファイルページではなくメディアファイルに直接リンクするようにします。
All hooks should be added to your custom SearchWP Customizations Plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Link directly to Media files instead of Attachment pages in search results | |
| // @link https://searchwp.com/documentation/knowledge-base/link-file-pdf/ | |
| function my_search_media_direct_link( $permalink, $post = null ) { | |
| if ( | |
| ( | |
| is_search() | |
| || doing_action( 'wp_ajax_searchwp_live_search' ) | |
| || doing_action( 'wp_ajax_nopriv_searchwp_live_search' ) | |
| || isset( $_REQUEST['swps'] ) | |
| ) | |
| && 'attachment' === get_post_type( $post ) | |
| ) { | |
| $permalink = wp_get_attachment_url( $post ); | |
| } | |
| return esc_url( $permalink ); | |
| } | |
| add_filter( 'the_permalink', 'my_search_media_direct_link', 99, 2 ); | |
| add_filter( 'attachment_link', 'my_search_media_direct_link', 99, 2 ); |

