Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom search.php
Tagged: relevanssi, search, search.php
- This topic has 7 replies, 2 voices, and was last updated 10 years, 6 months ago by cookieandkate.
-
AuthorPosts
-
March 9, 2014 at 11:26 pm #94126cookieandkateMember
Hi,
I'm really frustrated because I can hardly find any documentation on how to customize Genesis search results (this forum post only tells me how to remove post details from search results). I'm using Magazine Pro, which, by default, displays full posts in the search results. I want to show post excerpts instead, but changing the Genesis theme option to display excerpts throughout the site is not an option.
What I want in a search results page:
Post title
Post excerpt (called by the_excerpt, not the_content, so that Relevanssi works)
More linkI want to be able to specify the number of search results that display on the page. My default archive setting is to display six, but I want eighteen search results per page.
Can anyone help me with this? I would be so thankful.
Kate
March 11, 2014 at 4:22 am #94249WordpressMemberThis assumes HTML5 is enabled. Place this in your functions.php file:
// Set number of posts for search results to 18 add_action( 'pre_get_posts', 'child_search_posts_per_page' ); function child_search_posts_per_page( $query ) { if ( !is_admin() && $query->is_main_query() && $query->is_search() ) { $query->set( 'posts_per_page', 18 ); } }
Now copy
search.php
from Genesis parent theme (if you haven't already), and paste it into your Child template directory. In that search.php file put in the following code (right before thegenesis();
) Like so:// Remove post info & entry footer - Optional remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); remove_all_actions( 'genesis_entry_footer' ); // Excerpt and "read more" link remove_all_actions( 'genesis_entry_content' ); add_action( 'genesis_entry_content', 'child_do_post_excerpt' ); function child_do_post_excerpt() { the_excerpt(); echo '<a class="read-more-link" href="' . esc_url( get_permalink() ) . '">read more</a>'; } genesis();
Note: I haven't tested with the Relevanssi plugin.
March 11, 2014 at 10:43 am #94290cookieandkateMemberThat code works great. Thank you! Thank you so much.
Notes: I decided that I wanted the date and comments link to display, so I deleted that line. I found that deleting
remove_all_actions( 'genesis_entry_footer' );
made Relevanssi stop working.Lastly, I'm sorry, I forgot to mention that I would like for the post thumbnail to display under the title, with the post excerpt to the right of it. I tried adding
the_post_thumbnail('thumbnail');
above the_excerpt, but nothing happened. My post thumbnail size under media settings (which is the thumbnail I would like to display) is 130x179 px.Desired final result would look like my archive pages.
If you know how to display the thumbnail, I would be tremendously thankful if you could share the code.
Thank you!
KateMarch 11, 2014 at 10:49 am #94291cookieandkateMemberActually, strangely enough,
the_post_thumbnail('thumbnail');
does produce thumbnails in the search results. However, it doesn't produce a thumbnail next to each of my posts, only one here and there, which doesn't make sense because I have set a featured image for each post. Another problem is that the thumbnails don't include the "alignleft" class that they need in order to display the excerpt to the right, rather than underneath. Lastly, Relevanssi isn't working.I know that it must be possible to display the thumbnail next to the excerpt because that is how I had my search results set up on my former design.
March 11, 2014 at 10:57 am #94292cookieandkateMemberVictory!!! I figured it out by consulting the codex. The reason thumbnails weren't displaying next to all of the posts is because I'm testing the code on my test site and I haven't set thumbnails for all of the test site's posts.
Final code:
<?php add_action( 'genesis_before_loop', 'genesis_do_search_title' ); /** * Echo the title with the search term. * * @since 1.9.0 */ function genesis_do_search_title() { $title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s %s</h1></div>', apply_filters( 'genesis_search_title_text', __( 'Search Results for:', 'genesis' ) ), get_search_query() ); echo apply_filters( 'genesis_search_title_output', $title ) . "\n"; } // Remove post info & entry footer - Optional remove_all_actions( 'genesis_entry_footer' ); // Excerpt and "read more" link remove_all_actions( 'genesis_entry_content' ); add_action( 'genesis_entry_content', 'child_do_post_excerpt' ); function child_do_post_excerpt() { the_post_thumbnail('thumbnail', array('class' => 'alignleft')); the_excerpt(); echo '<a class="read-more-link" href="' . esc_url( get_permalink() ) . '">Continue reading ยป</a>'; } genesis();
March 11, 2014 at 7:19 pm #94346WordpressMember^ Looks good. That's done.
As for the plugin, I've tried and it works. I can see search terms highlighted. Adding/removing code didn't affect it. I can only suggest the obvious like reinstalling the plugin or rebuilding the index. Not much else I can do there sorry.
March 11, 2014 at 7:57 pm #94358cookieandkateMemberSorry for the confusion. The plugin is working great with the code I provided above.
March 11, 2014 at 7:57 pm #94359cookieandkateMemberP.s. Thanks again!
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.