• Resolved Nick Ottens

    (@ottens)


    I think this problem is due to infinite scroll.

    I created a separate template for my search results in order to show only the title, meta data and summary.

    The relevant code from my search.php looks like this:

    <?php /* Start the Loop */ ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php get_template_part( 'content', 'search' ); ?>
    
    <?php endwhile; ?>

    And then the template is in content-search.php.

    This works as expected on the first “page” of search results, but as soon as you scroll down and “Page 2” loads, the posts switch to the regular template.

    Any ideas on how to prevent this?

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Sorry about the delay in writing back to you.

    Could you try to deactivate the Infinite Scroll feature under Jetpack > Settings in your dashboard, and let me know if the problem then disappears?

    Thread Starter Nick Ottens

    (@ottens)

    Thanks for your reply!

    Yes — if I disable Infinite Scroll, the problem disappears.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Thanks for giving it a try. What happens if you switch to one of the default themes, like Twenty Sixteen, for a few minutes? Does the problem remain?

    Thread Starter Nick Ottens

    (@ottens)

    Twenty Sixteen displays all the search results the same.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Alright. Could you check the render function you’ve created when declaring support for Infinite Scroll, and add a conditional checking for is_search and then loading the template you use to display search results? That should solve your issue.

    Thread Starter Nick Ottens

    (@ottens)

    I’m not sure what you mean?

    This is the code I put in my functions file to enable Infinite Scroll:

    	 /*
    	 * Enable infinite scroll
    	 */
    	 add_theme_support( 'infinite-scroll', array(
    		'container' => 'main',
    		'footer' => 'page',
    	 ) );
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    I would suggest reviewing this support doc:
    https://jetpack.com/support/infinite-scroll/

    You’ll find a parameter named render that you can use to force the search result page to use the right template.

    I hope this helps.

    Thread Starter Nick Ottens

    (@ottens)

    Sorry — I’m afraid I’m not enough of a coder to understand.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Sorry about that. You can check how we do things for other themes to understand how this works:
    https://github.com/Automattic/jetpack/blob/master/modules/infinite-scroll/themes/twentysixteen.php#L20-L32

    Thread Starter Nick Ottens

    (@ottens)

    Here’s what I put in my functions file, but the problem remains, I’m afraid:

    	 /*
    	 * Enable infinite scroll
    	 */
    	 add_theme_support( 'infinite-scroll', array(
    		'container' => 'main',
    		'render'    => 'jetpack_sentinel_infinite_scroll_render',
    		'footer' => 'page',
    	 ) );
    
    /**
     * Custom render function for Infinite Scroll.
     */
    function jetpack_sentinel_infinite_scroll_render() {
    	while ( have_posts() ) {
    		the_post();
    		if ( is_search() ) {
    			get_template_part( 'template-parts/content', 'search' );
    		} else {
    			get_template_part( 'template-parts/content', get_post_format() );
    		}
    	}
    }
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Does get_template_part( 'template-parts/content', 'search' ); seem to load the right template now?

    Thread Starter Nick Ottens

    (@ottens)

    No, it still loads the wrong one starting from Page 2.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Could you double check and make sure your search template loop is available at wp-content/themes/yourtheme/template-parts/content-search.php, or update the function to load the right template? In your first post you mentioned get_template_part( 'content', 'search' );, so your search template file might be at wp-content/themes/yourtheme/content-search.php.

    Thread Starter Nick Ottens

    (@ottens)

    Yes, I realized that and changed it to get_template_part( 'content', 'search' ); after I posted — but no effect.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    At this point it might be useful to use the Debug Bar to check what templates are loaded on each page, and make sure you are all set there. That should clarify things a bit.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Search results switch template on Page 2’ is closed to new replies.