Skip to content

Instantly share code, notes, and snippets.

@justinswelch
Last active March 2, 2018 19:47
Show Gist options
  • Save justinswelch/77c5c02f5bb40c0b652f86f6c0ab9ff6 to your computer and use it in GitHub Desktop.
Save justinswelch/77c5c02f5bb40c0b652f86f6c0ab9ff6 to your computer and use it in GitHub Desktop.
<?php
/**
* Customized archive template
* Intended to be used in a child theme as index.php
* Outputs a Beaver Builder posts module for archive pages
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package pixablaze_base
*/
/********** Configuration ************
* Purpose: set archive page to use a saved Beaver Builder template
* Create a saved BB template using a standard Posts module (change ID below)
* In the BB posts module, use default query settings (this should override it)
************************************/
$bb_archive_id = 28; // edit to match the ID of the saved template or row
/************ Filter posts **********
* Filter the Beaver Builder $query for the posts module on archive pages
* Note: this filter is here for a convenient view of related functions.
* this would typically be in functions.php or a custom plugin.
************************************/
add_filter( 'fl_builder_loop_query', 'pxb_bb_category_archives', 10, 2 );
function pxb_bb_category_archives( $query, $settings )
{
// if not on a category or tag page, return the default query
if( ! is_category() && ! is_tag() ) return $query;
// get all the query args so we can return the whole object
$query_args = $query->query_vars;
// set the category and tag ids and names from the current post
// this filters the BB posts module settings depending on which archive is being viewed
$query_args[ 'cat' ] = get_query_var( 'cat' );
$query_args[ 'category_name' ] = get_query_var( 'category_name' );
$query_args[ 'tag' ] = get_query_var( 'tag' );
$query_args[ 'tag_id' ] = get_query_var( 'tag_id' );
// return the filtered query
return new WP_Query( $query_args );
}
/************************************
* Start the template
***********************************/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Check if Builder is activated and the supplied post ID exists
$test_post = get_post( $bb_archive_id );
if ( !class_exists( 'FLBuilderModel' ) || !is_object( $test_post ) ) {
// something is wrong, use default loop
get_template_part( 'template-parts/content', get_post_format() );
} else {
// Post exists and Builder is active - output the BB layout
echo do_shortcode( '[fl_builder_insert_layout id="'. $bb_archive_id. '"]' );
} ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment