Set Default and Min Value
November 12, 2024 ยท AI Conversations
'number_of_posts',
[
'label' => __('Number of Jobs', 'rosemeier'),
'type' => E lementorControls_Manager::NUMBER,
'default' => __( 'Default Title', 'sage' ),
]
);
add default value 10 and min 5
### Updated Code
$this->add_control(
'number_of_posts',
[
'label' => __('Number of Jobs', 'rosemeier'),
'type' => ElementorControls_Manager::NUMBER,
'default' => 10, // Set default value to 10
'min' => 5, // Set minimum value to 5
]
);
### Explanation:
– **`default`**: Sets the default value to `10` instead of `'Default Title'`.
– **`min`**: Ensures the user cannot select a value lower than `5`.
This control will now show a default value of `10` and restrict the minimum value to `5` when adjusted in the Elementor editor.
'post_type' => 'resource',
'orderby' => 'title',
'order' => 'ASC',
);
$query = new WP_Query($args);
pass number_posts
### Updated Code
// Get the value of 'number_of_posts' from the Elementor settings.
$number_of_posts = !empty($settings['number_of_posts']) ? $settings['number_of_posts'] : 10;
// Define the WP_Query arguments.
$args = array(
'post_type' => 'resource',
'posts_per_page' => $number_of_posts, // Use the control value here.
'orderby' => 'title',
'order' => 'ASC',
);
// Execute the query.
$query = new WP_Query($args);
### Explanation:
– **`$settings['number_of_posts']`**: Retrieves the value set in the Elementor control.
– **`posts_per_page`**: Limits the number of posts retrieved by the query.
### Handling Defaults:
– If the control value is not set, a default of `10` is used.
– Ensure that your Elementor widget code properly passes the `$settings` array to where you're running this query.
Make sure to replace `$settings` with the actual variable used in your Elementor widget class if it's named differently.