Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author htmlBurger

    (@htmlburger)

    Howdy @knath632,

    Sidebars registered with Carbon Fields’s sidebar manager are registered with a slug version of the provided sidebar name. The slug version is generated by the sanitize_title() built-in WordPress function, which is also used to generate the slugs of the posts/pages.

    So for example if your sidebar is called Test Sidebar, its autogenerated slug will be test-sidebar.

    In order to render that sidebar in your code, consider the following code:

    <?php if ( is_active_sidebar( 'test-sidebar' ) ) : ?>
    	<ul class="sidebar">
    		<?php dynamic_sidebar( 'test-sidebar' ); ?>
    	</ul>
    <?php endif; ?>

    Please, don’t hesitate to ask if you have any other questions.

    Thread Starter knath632

    (@knath632)

    Hi htmlBurger,

    Thanks for your reply. I thought may be there is a code by which I can access all the sidebar which is created on the fly.

    Like if I create Test Sidebar1, Test Sidebar2 and so on but by one certain code these will appear without writing separately.

    As a developer it is okay to create like that way but when it will be deliver as a product to client then client may create some side bars but it would not appear without writing code like you provided.

    Is there any possibilities like that? Please let me know.

    Plugin Author htmlBurger

    (@htmlburger)

    Hey @knath632,

    Yes, the Sidebar Manager is a completely usable feature that allows you to create, manage and render dynamic sidebars.

    The custom sidebars that are created on-the-fly are intended to be used with the Sidebar field: https://carbonfields.net/docs/fields-sidebar/

    This field allows you to create new sidebars, or to select any sidebar (either created on-the-fly, or registered in the code), and then in your templates you can use the value of that field in the dynamic_sidebar() function. This will display sidebar that is selected in the field.

    Also, in case you want to retrieve all existing sidebars (as you asked), you can do it the following way:

    $sidebars = apply_filters( 'carbon_custom_sidebars', get_option( 'carbon_custom_sidebars', array() ) );

    Please, let me know if you have any other questions.

    Thread Starter knath632

    (@knath632)

    Thanks, I was looking for it, But I am getting like following

    Array
    (
    [about-sidebar] => Array
    (
    [id] => about-sidebar
    [name] => About Sidebar
    )

    [my-new-sidebar] => Array
    (
    [id] => my-new-sidebar
    [name] => My New Sidebar
    )

    )

    Please let me know how can I get elements inside it? like Text widget, calendar, recent posts etc.

    Plugin Author htmlBurger

    (@htmlburger)

    Hi @knath632,

    Do you see the sidebar ID? It is about-sidebar or my-new-sidebar. You can use it in the dynamic_sidebar() and is_active_sidebar() functions, as in the example we’ve posted above.

    However we feel you want to dynamically choose a sidebar which to display. If that is the case, you should do the following:

    1. Create a container (for example theme options). Example and docs: https://carbonfields.net/docs/containers-theme-options/
    2. Add a Sidebar field, named custom_sidebar: https://carbonfields.net/docs/fields-sidebar/
    3. Go to the admin and select your sidebar in that field. You can create new ones, if you wish.
    4. Use the following code to display that dynamic sidebar (assuming your field is in a Theme Options container and is named custom_sidebar):

    <?php $sidebar = carbon_get_theme_option( 'custom_sidebar' ); ?>
    <?php if ( is_active_sidebar( $sidebar ) ) : ?>
    	<ul class="sidebar">
    		<?php dynamic_sidebar( $sidebar ); ?>
    	</ul>
    <?php endif; ?>

    Hope that answers your question.

    Thread Starter knath632

    (@knath632)

    Thanks. I just changed carbon_get_theme_option to carbon_get_post_meta and it works ??

    Thread Starter knath632

    (@knath632)

    Hi htmlBurger,

    I hope you are doing well. I need to know that the dynamic sidebar is not updated after changing to custom sidebar in Blog Page which one created on the fly in the widget section.

    What would be the code. for example if we have to set default thene we are using ->show_on_template(‘default’), now please let me know what would be the code fro Blog page?

    Plugin Author htmlBurger

    (@htmlburger)

    Hi @knath632,

    Can you please provide more details about what you need to do, and probably some more code?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Sidebar on the fly’ is closed to new replies.