Best way to programmatically render a beaver template (example : archives pages)

Hi,

We would like to use the power of beaver builder into archives pages.

In a subtheme, for example into archive-{posttype_slugname}.php, we could test if a beaver template with the same name exists. If it exists, we could render this beaver template.

What is the best way to render a beaver template in PHP ?

Thank you in advance

1 Like

Hey novfr,

That’s an interesting solution. You can pull in the HTML for a Beaver Builder layout the same way you would any other post, just make sure you’re using the_content within a loop.

In order to get the CSS and JS assets to load, you’ll need to use the fl_builder_global_posts filter.

Let me know if you have any questions!

Justin

Thanks for your answer, we will try asap and we will keep you informed.

Hi,

It works with any custom post type but not with fl-builder-template type

In our single-sycrm-contact.php file :


<?php get_header(); ?>

<div class="container">
<div class="row">
    
    <?php FLTheme::sidebar('left'); ?>

    <?php
    // WP_Query arguments
    $args = array (
        'name' => 'sycrm-contact',
    	'post_type' => 'fl-builder-template',
    );
    
        // The Query
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ($query->have_posts()) {
        		$query->the_post();
        		echo the_content();
        } 
    }
    ?>

    <?php FLTheme::sidebar('right'); ?>
</div>
    
<?php get_footer(); ?>

the_content is empty with fl-builder-template. the_content is not empty with a page type for example.

Hey novfr,

Are you adding the post ID for that post using fl_builder_global_posts?

Justin

Hey there,

Sorry to piggy-back on this thread, but was it ever resolved? If so, what was the solution?

Unless I’m not understanding correctly, this would allow me to pull in a BB Template into a hard-coded template?

Thanks in advance – I am looking to do the very same thing.

  • Desmond

Hey novfr, can you chime in here?

Desmond, I’m not 100% sure what novfr is trying to do, so hopefully he can shed a little light on that.

Justin

Thanks Justin,

Being able to create a BB template part (such as a masthead) and then being able to pull that into a hard-coded template so that all posts sharing that hard-coded template (such as a blog) would always look consistent (rather than using BB on a post by post basis) would be awesome.

novfr, looking forward to your response!

Thanks guys.

Hey Desmond,

Thanks for the additional info. I believe novfr is trying to do something similar, essentially bringing a Beaver Builder layout into another page.

You should be able to use that by querying the post and using the_content function. You’ll also want to have a look at the fl_builder_global_posts filter.

Let me know how it goes.

Justin

Hey Desmond,

Thanks for the additional info. I believe novfr is trying to do something similar, essentially bringing a Beaver Builder layout into another page.

You should be able to use that by querying the post and using the_content function. You’ll also want to have a look at the fl_builder_global_posts filter.

Let me know how it goes.

Justin

I was able to hack together the following and it seems to be working (I also needed to add the fl_builder_global_posts filter):

<?php 

/*
Template Name: Single Propulsion Post
*/

get_header(); 

?>

<?php 

$my_query = new WP_Query("post_type=page&page_id=23");
while ($my_query->have_posts()) : $my_query->the_post();?>

            	<?php the_content(); ?>

<?php endwhile; ?>

<div class="container">
	<div class="row">
		
		<?php FLTheme::sidebar('left'); ?>
		
		<div class="fl-content <?php FLTheme::content_class(); ?>">
			<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
				<?php get_template_part('content', 'page'); ?>
			<?php endwhile; endif; ?>
		</div>
		
		<?php FLTheme::sidebar('right'); ?>
		
	</div>
</div>

<?php get_footer(); ?>

Basically I would need to create a page that acts as a template part, then pull that in. Doesn’t seem very elegant, but I’m not sure what else I could do.

I think novfr’s idea of pulling in the name of a custom BB template is more ideal, but I couldn’t get it working. If there are any ideas out there, just let me know!

  • Desmond

Hi,

A proof of concept :

In single.php :

<?php if (class_exists( 'FLBuilder' ) && (get_post_meta( get_the_ID(), '_fl_builder_enabled', true ) || isset( $_GET['fl_builder'])) || get_post_meta(get_the_ID(),'_views_template', true) != 0) : ?>

    <?php get_header(); ?>

    <div class="fl-content-full container">
        <div class="row">

            <div class="fl-content <?php FLTheme::content_class(); ?>">
                <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
                    <?php the_content(); ?>
            	<?php endwhile; endif; ?>
            </div>
            
        </div>
    </div>
    
    <?php get_footer(); ?>

<?php else : ?>
       
    <?php $query = new WP_Query( array ( 'name' => get_post_type(), 'post_type' => 'template', ) ); ?>
    <?php if ( $query->have_posts() ) : ?>
        <?php get_header(); ?>        
        <?php while ($query->have_posts()) : $query->the_post(); ?>
        

            <div class="fl-content-full container">
                <div class="row">
                    <div class="fl-content <?php FLTheme::content_class(); ?>">
                        <?php echo the_content() ?>
                    </div>
                </div>
            </div>

        <?php endwhile; ?>
        <?php get_footer(); ?>        
        <?php  wp_reset_postdata(); ?>
    <?php else: ?>
        <?php require_once(ABSPATH . 'wp-content/themes/bb-theme/single.php'); ?>
    <?php endif; ?>
<?php endif; ?>

This is an example for a single post : In template we use shortcode to display title, fields, and featured image.

We will do the same thing into archive.php to customize appareance of archives pages

Hey novfr,

Thanks so much for posting that code. I’m more of a front-end developer, so I’m having a little trouble wrapping my head around what you posted.

Basically I want to pull in a saved BB template (let’s say it is named “masthead”) into a custom post type template (called single-propulsion.php).

So really the structure would be:

Header
Pulled in BB template named “masthead”
Content of custom post type entry
Footer

Any hints?

Thanks again,

Desmond

I’m not sur I understand : do you want to load masthead template into a part of a wordpress template ?

If that is, you can echo in this template the content of masthead loaded with a WP_Query

Hi novfr,

Thanks for the reply. I guess I’m trying to achieve what I believe you wanted to do in your original post, which is to call a “fl-builder-template” by it’s name. Something like this:

<?php 

/*
Template Name: Propulsion Post
*/

get_header(); 

?>

<?php 
    // WP_Query arguments
    $args = array (
        'name' => 'masthead',
    	'post_type' => 'fl-builder-template',
    );
    
        // The Query
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ($query->have_posts()) {
        		$query->the_post();
        		echo the_content();
        } 
    }
?>

<div class="container">
	<div class="row">
		
		<?php FLTheme::sidebar('left'); ?>
		
		<div class="fl-content <?php FLTheme::content_class(); ?>">
			<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
				<?php get_template_part('content', 'single'); ?>
			<?php endwhile; endif; ?>
		</div>
		
		<?php FLTheme::sidebar('right'); ?>
		
	</div>
</div>

<?php get_footer(); ?>

Am I missing a critical piece of code? Or perhaps it isn’t possible?

We finally decided to create our own custom post type named “template” to customize archive and single content (article, taxonomy, etc.). But it’s possible to use the official CPT of beaver.

Thanks novfr,

I’m having trouble pulling data from “fl-builder-template”, but I’ll play around and see what I can come up with. Thanks again for the insight.

Hi Desmond,

You should be able to do something like this (untested)…

query_posts( array(
   'post_type' => 'fl-builder-template',
   'p' => 123 // Your post ID.
) );

if( have_posts() ) { 
   	while( have_posts() ) {
		      the_post();		
      		the_content();
   	}
}

wp_reset_query();

Let me know if you have any questions.

Justin

Hmm, still not pulling any data from “fl-builder-template”. I’m not sure what the issue is. I’ll stop bugging you guys, but if you happen to get the itch to find a working solution for what I want to do, just let me know!

Thanks and have a great weekend.

  • Desmond