-
-
Save searchwpgists/9c1fa6434dc92ccdfc61f6c915b08db7 to your computer and use it in GitHub Desktop.
Prepend post type label to title for native main search
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 | |
| // Prepend post type label to title for native main search | |
| add_filter( 'the_title', function( $title, $post_id ) { | |
| if ( in_the_loop() && ( is_search() || isset( $_REQUEST['swps'] ) ) ) { | |
| $post_type_obj = get_post_type_object( get_post_type( $post_id ) ); | |
| if ( $post_type_obj && ! empty( $post_type_obj->labels->singular_name ) ) { | |
| $label = esc_html( $post_type_obj->labels->singular_name ); | |
| $title = sprintf( '<span class="search-post-type">%s:</span> %s', $label, $title ); | |
| } | |
| } | |
| return $title; | |
| }, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment