Kirki: Category dropdown default

Created on 30 Oct 2017  路  12Comments  路  Source: kirki-framework/kirki

Issue description:

I have a theme feature where the user can set four featured posts based on category, using the dropdown 'choices' => Kirki_Helper::get_terms( 'category' ). I am running a post query based on what is selected is in the dropdown. I don't want to set a default for this field as it's unknown and based on unique categories the user has in their blog.

The dropdowns initially all display a category, but the post query doesn't work until the user selects a different cat in the dropdown. If they want the initially displayed category, they have to first select a different category and then select the initial category again.

This feels like quite bad UX and it'd be nice to either have the option for a placeholder in the dropdown, e.g. 'Select a category'. Is this something that is possible or do I need to approach this in a different way?

Code to reproduce the issue (config + field(s))

$fields[] = array(
    'settings'    => 'feat_cat_1',
    'type'        => 'select',
    'label'       => __( 'Choose a category', 'brio-helper' ),
    'section'     => 'featured_cat_posts',
    'default'     => '',
    'choices'     => Kirki_Helper::get_terms( 'category' ),
);

function shortened for brevity

if (!function_exists('get_cat_post')) { 
    function get_cat_post($category) {

        $cat_query = new WP_Query( array(
            'post_type'             => 'post',
            'showposts'             => 1,
            'ignore_sticky_posts'   => true,
            'cat'                   => $category,
            )
        );

        while ( $cat_query->have_posts() ): $cat_query->the_post();

                <h3 class="widget-title"><span><?php echo get_cat_name($category); ?></span></h3>
                // some other stuff
        <?php
        endwhile; wp_reset_query();
    }
}
if (!function_exists('brio_featured_cat_post')) { 
    function brio_featured_cat_post() {

        $post_cat_1 = get_theme_mod('feat_cat_1');
        $post_cat_2 = get_theme_mod('feat_cat_2');
        $post_cat_3 = get_theme_mod('feat_cat_3');
        $post_cat_4 = get_theme_mod('feat_cat_4');

        ?>

        <div class="o-grid">

            <?php if($post_cat_1) { get_cat_post($post_cat_1); } ?>
            <?php if($post_cat_2) { get_cat_post($post_cat_2); } ?>
            <?php if($post_cat_3) { get_cat_post($post_cat_3); } ?>
            <?php if($post_cat_4) { get_cat_post($post_cat_4); } ?>

        </div>
    <?php
    }
    add_action('brio_content_before','brio_featured_cat_post',5);
}
select feature

All 12 comments

I had a similar problem but for the default I just picked an arbitrary value from my values. If you don't know what any of the categories are going to be you could create a function to pick the first one from the list and return it.

so maybe you could do :

$defaults = array_keys(Kirki_Helper::get_terms( 'category' ));
$default = $defaults[0];

Adding as a feature request for a placeholder.

Same issue here, with a featured category dropdown that ideally should have nothing selected so that it displays the latest posts from all categories if a specific category is not selected.

On output you can already do whatever you want...
You just need to check the value first before getting the posts.
Example:

$args = array(
    'posts_per_page'   => 5,
    'category'         => '',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_status'      => 'publish',
);
$selected_category = get_theme_mod( 'my_category_setting' );
if ( $selected_category && null !== get_category( $selected_category ) ) {
    $args['category'] = $selected_category;
}
$posts_array = get_posts( $args );

The above example sets some default arguments.
Then we check if a category is set in the select. If there is a selection and the category exists, we change the category argument to the category selected.
Then, those arguments are passed-on to the get_posts function (or WP_Query or whatever you want to use) and we get the posts.

Fixed, will be included in v3.0.21.
select controls now have a placeholder argument you can use.
See example: https://github.com/aristath/kirki/blob/12c85c3fe0a49e830873f1e983d283c57852d217/example.php#L719-L736

:+1: Great stuff.

Is the placeholder option going to work with the dropdown for sidebars as well (the code that you gave me)?

the placeholder will work for all select controls, regardless of what options those controls contain. So yes, for the sidebars one as well - provided of course you add the placeholder argument.
If you want it empty by default, then set 'default' = > '' too 馃槈

Sweet 馃憤

When do you recon 3.0.21 will be out?

-5 minutes

:D, brilliant. Working great with placeholder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manuelmoreale picture manuelmoreale  路  3Comments

Radzio1615 picture Radzio1615  路  4Comments

rinkuyadav999 picture rinkuyadav999  路  4Comments

ravishakya picture ravishakya  路  5Comments

stylethemes picture stylethemes  路  6Comments