Use Template as Header; CSS Missing

Hi,

I’m trying to use the Templates posts in place of headers and footers. I’m using the get_post() function, which kind of works. The function returns the HTML of the template, but not the CSS styles. Could anyone enlighten me how to do this properly?

Thanks,
Marc

Hey Marc,

Welcome to BB Forums! :slight_smile: Just letting you know I’m checking in with the team on this one.

Thanks!

KC

Hey KC,

Thanks for the heads up. If have been trying to figure this out for the last couple of hours but no luck. The best I could do as of now is to modify the main query and reset it again to get the complete HTML code of the template. However, it seams that the specific CSS for the template is not being loaded along with it.

query_posts( array(
    'post_type' => 'fl-builder-template',
    'post_status' => 'any',
    'p' => 68, // post id
) );
while (have_posts()) : the_post();
    the_content();
endwhile;
wp_reset_query();

Hey Marc,

The issue is most likely that the CSS/JS for the layout your are rendering via a secondary loop isn’t being loaded. Have a look at the fl_builder_global_posts filter to fix that…

http://forum.wpbeaverbuilder.com/knowledge-base/filter-reference/

Let me know if you have any questions.

Justin

Hi Justin,

Thanks for helping out. Sorry though, the filter doesn’t make a difference. The snippet I posted above is living in my header.php and I tried use the filter you mentioned inside the functions.php file.

BB is enqueuing the stylesheet for the respective page ID, but I can’t find the node-id inside of it and there is also no other stylesheet for the fl-builder-template being enqueued.

I tried clearing my browser’s and BB’s cache but it didn’t solve my problem either.

Thanks again for helping me with this.

Marc

Hey Marc,

Would it be possible for you to share temp admin access to the site so we can take a look? You can just post the details here using the private reply option below.

Ben

Howdy Ben,

The site is in local development and it’s currently just a copy of the twentytwelve theme which I’m using for prove of concept.

Here are the code snippets I’m using:

header.php:

query_posts( array(
    'post_type' => 'fl-builder-template',
    'post_status' => 'any',
    'p' => 68, // post id
) );
while (have_posts()) : the_post();
    the_content();
endwhile;
wp_reset_query();

functions.php:

function my_global_builder_posts( $post_ids ) {
    $post_ids[] = '68';
    return $post_ids;
}
add_filter( 'fl_builder_global_posts', 'my_global_builder_posts' );

Marc

Hey Marc,

Just letting you know we’re checking into this.

Ben

Hey Marc,

It seems we’re having issues with the filter when used on templates. I’ve filed a bug report so our lead dev can check. Thanks for the heads up! :slight_smile: For now, you can just use a page or post instead of a template.

Ben

Hey Marc,

A fix for this issue will be available in the minor update next week. For now, you can patch it if you would like by making a change to classes/class-fl-builder.php. Just add the following code after line 369 in the layout_styles_scripts method and it should start working…

// Enqueue assets for templates since they won't be returned in the query above.
$posts = get_posts(array(
	'post__in' 		=> $post_ids, 
	'post_type' 		=> 'fl-builder-template',
	'posts_per_page'	=> -1
));

foreach($posts as $post) {
	self::enqueue_layout_styles_scripts($post->ID);
}

Let me know if you have any questions about that.

Justin

NOTE: I posted the following before refreshing the page. Thank you Justin. I’ll try that and I’m looking forward to the update. You guys rock!

Thanks for passing it on Ben. I tried the using the “page” post_type inside of the query_posts() as mentioned above, but it’s also not working as expected. Am I doing it wrong? If not, maybe update your bug report accordingly. Thanks again for your support.

Justin, will the update enable the use of other post types as well? For example, if I were to register a post type called “Components” that I wanted to use instead of the “fl-builder-template” post type. Would that also work?

Ups, sorry Ben. Including a page instead of the fl-builder-temple is working. Never mind my previous post.

Hey Justin, I was pretty tired when I posted my previous reply to you. To clarify my question; I would not want my “components” post-type to show up in the search results. However, the function get_posts( ‘post_type’ => ‘any’ ) on line 363 inside FLBuilder::layout_styles_scripts() function will only get types that have the parameter “exclude_from_search” NOT set to true. I think such a post-type should not belong inside the search results and this could be solved better. Thanks.

Hey Marc,

Not having the post types show up in search is the issue as post_type => any won’t return them. That’s why I’m doing an additional query of post_type => fl-builder-template, but I didn’t think about other post types that aren’t included in search.

Let me chew on this one a bit more and get back to you.

Justin

As an alternative, a hook could also do the trick.

Hey Marc,

I did some more testing and found a solution that should work in all cases without the need to do anything additional. First, scrap the code I gave you previously. Then, within that same class, change this…

$posts = get_posts(array(
	'post__in' 		=> $post_ids, 
	'post_type' 		=> 'any',
	'posts_per_page'	=> -1
));

to this…

$posts = get_posts(array(
	'post__in' 		=> $post_ids, 
	'post_type' 		=> get_post_types(),
	'posts_per_page'	=> -1
));

Give that a shot and let me know how it goes.

Thanks,
Justin

Ha! Works like charm! I did some speedy testing and it seams that now the only 2 required parameters for the register_post_type() function are:

'publicly_queryable' => true, & 'query_var' => true,

Thanks for that.

Now, the only other thing on my mind is the poor performance of query_posts(). From the codex: “It is inefficient (re-runs SQL queries) and will outright fail in some circumstances…” Any ideas or plans?

Thanks again for the prompt help here Justin!

Great!

Regarding, query_posts, we’re actually using get_posts which I believe doesn’t alter the main query or have the same issues as query_posts. You could try running get_posts in your code as well, it should work.

Justin

Hey Justin,

Thanks yeah it’s working. The first time I tried I forgot to run it through “the_content” filter. I posted the code below in case others find themselves here. Thanks again for all your help.

$post = get_posts( array(
    'post_type' => 'components',
    'post_status' => 'any',
    'p' => 93, // post_id
) );
$post = $post[0];
echo apply_filters( 'the_content', $post->post_content );