Custom search.php

Community Forums Forums Design Tips and Tricks Custom search.php

This topic is: resolved

This topic contains 7 replies, has 2 voices, and was last updated by  cookieandkate 7 months, 3 weeks ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #94126

    cookieandkate
    Member
    Post count: 60

    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 link

    I 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

    #94249

    Wordpress
    Member
    Post count: 37

    This 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 copysearch.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 the genesis();) 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.

    #94290

    cookieandkate
    Member
    Post count: 60

    That 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 130×179 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!
    Kate

    #94291

    cookieandkate
    Member
    Post count: 60

    Actually, 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.

    #94292

    cookieandkate
    Member
    Post count: 60

    Victory!!! 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();
    
    #94346

    Wordpress
    Member
    Post count: 37

    ^ 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.

    #94358

    cookieandkate
    Member
    Post count: 60

    Sorry for the confusion. The plugin is working great with the code I provided above.

    #94359

    cookieandkate
    Member
    Post count: 60

    P.s. Thanks again!

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.