Conditionnal depending on Beans layout


Hello Thierry !

In Beans, how would you set a conditionnal action based on the type of layout choosen by the user while creating his post (fullwidth, sidebar-left, sidebar-right, etc.) ? Such as :

<?php

add_action( 'wp', 'myprefix_custom_output' );

function myprefix_custom_output() {
    if ( THE TYPE OF LAYOUT  ) {

   Change the markup accordingly

    }
}

Thanks a lot ! And have a nice day. :)


EDIT I've tried with this :

if ( beans_get_layout() == 'c' )

But with no success...

Mathieu

Edited 1 year ago by Mathieu Rouault


Hey Mathieu,

beans_get_layout() is the correct function to use and the wp should work perfectly fine. On which layout are you using this (if you have a link to the page it would be even better)?

Thanks,


Hello Thierry, My bad, it perfectly works indeed, sorry for the inconvenience ! :-( Mat


I experienced the same problem as Mathieu... Here's my code:

add_action( 'wp', 'center_page_content' );
function center_page_content() {
    if ( beans_get_layout() == 'c' ) {
        beans_add_attribute( 'beans_fixed_wrap[_main]', 'class', 'tm-centered-content' );
    }
}

but nothing happened. I tried use this snippet inside functions.php and page.php


Hi Mariusz,

One of the most important (and trickiest) thing to understand and remember when working with WordPress actions is when the main query and actions run. For instance, the action wp is called after the child theme functions.php runs but before the template file runs.

As most things on a page depends on the WP_Query, for instance Beans layout, it is also important to understand that it runs after functions.php, before the wp action runs and of course before the template file loads (since the template file is set using the WP_Query).

So let's make sense of all this. Because the WP_Query runs after functions.php and Beans layout is defined after WP_Query we can not not the following in functions.php, we have to run our Beans layout conditional check in a wp action callback which will run after the WP_Query and Beans layout are set, as follow:

add_action( 'wp', 'example_callback' );

function example_callback() {
    if ( beans_get_layout() == 'c' ) {
        // Do Something.
    }
}

This is all good in functions.php and it works like a charm. That said in template file it won't work because the wp action already ran when the template file loads. But if we think about it, we don't need to use the wp action since the WP_Query and Beans layout are already set when the template file loads. So instead, we can do our check immediately as follow:

if ( beans_get_layout() == 'c' ) {
    // Do something.
}

So the snippet above will work in your page.php 😉

I hope that makes a bit more sense!

Happy coding,

Write a reply

Login or register to write a reply, it's free!