User-defined variable for template/shortcode possible?

Hello,

I have a question regarding Bricks Builder. Overall, I like Bricks Builder, but I’m wondering if it’s possible to pass “variables” to templates. For example, I’d like to display a list of posts, but I want to adjust the number of queried entries depending on where the template is embedded. On the homepage, for instance, I’d like to show 5 posts, whereas under a single post, I’d like to show 10 — using the same template. In other words, I want to avoid creating two separate templates for this.

In WordPress, you can use shortcodes with variables; could this work here, for example, by doing something like [bricks_template id=“358” query_amount=5]?

Is there any way to achieve this?

Thanks!

Hi Silas,
Welcome to the forum!

You can use the query_vars filter in your child theme’s functions.php or a code snippets plugin to accomplish this task: Filter: bricks/posts/query_vars – Bricks Academy

Use the template element to include your loop template on the homepage and your single post template, and note the loop ID.

Something like this should work (make sure to change the $element_id accordingly to the template’s loop ID):

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    // Check for homepage
    if ($element_id == 'virrdr' && is_front_page() ) {
        $query_vars['posts_per_page'] = 5;
        $query_vars['ignore_sticky_posts'] = 1;
    }

    // Check for single post
    if ($element_id == 'virrdr' && is_single()) {
        $query_vars['posts_per_page'] = 10;
        $query_vars['ignore_sticky_posts'] = 1;
    }

    return $query_vars;
}, 10, 3 );

Best,
timmse