Algolia Community

Replace WordPress archive template with Algolia/Instantsearch

wordpress

#1

Because of some location related needs for our category pages for our custom post types, using Algolia made more sense than just using WordPress’ built in archive template.

We’ll call our custom post type “program” for this implementation example. The normal WordPress archive template would be “archive-program.php” to do custom layout for that post type.

The first step is to add a filter to replace the template:

add_filter( ‘archive_template’, ‘archive_programs_instant_search’ );
function archive_programs_instant_search( $archive_template ) {
global $post;
if ( $post->post_type == ‘listing’ ) {
$archive_template = get_stylesheet_directory() . ‘/algolia/instantsearch.php’;
}
return $archive_template;
}

The second step is to load the Algolia/instantsearch styles and scripts. This code is assuming you have copied the “instantsearch.php” file from the plugin, into your custom theme directory. There is no direct way to do this, so you need to access the Algolia plugin directly. Then you need to enqueue the styles and scripts:

Algolia_Plugin::register_assets();
// Enqueue the instantsearch.js default styles.
wp_enqueue_style( ‘algolia-instantsearch’ );

// Enqueue the instantsearch.js library.
wp_enqueue_script( ‘algolia-instantsearch’ );

You may want to setup some way to only enqueue the styles and scripts if necessary.

That should be all you need to do.


#2

@matt7 did these steps work for you? am thinkingof doing the same thing to get a instant search input results on an archive page.