Enlace al archivo (por ejemplo, PDF) en lugar de la página de adjunto
WordPress, por defecto, enlazará a una página de adjunto para cualquier medio devuelto como resultado de búsqueda. Muchos temas no tienen plantillas útiles para medios y muy a menudo tiene sentido enlazar directamente al archivo en sí.
Este fragmento personalizará la plantilla de sus resultados de búsqueda para enlazar directamente a archivos multimedia en lugar de a una página de adjunto.
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?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 ); |

