I have created an elementor SELECT2 Control and now I want to display selected categories post Title and thumbnail
I have created a custom post type which is ‘post_type’ => ‘video’ in my control displayed all the video categories now i want to display all selected that posts Title and Thumbnail in Elementor content_template() and render() function..
<?php
$options = array();
$args = array(
'hide_empty' => false,
'post_type' => 'video',
'taxonomy' => 'video_categories',
);
$categories = get_categories($args);
foreach ( $categories as $key => $category ) {
$options[$category->term_id] = $category->name;
}
$this->add_control(
'video_categories',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => ElementorControls_Manager::SELECT2,
'multiple' => true,
'options' => $options,
]
);
}
// Want to display selected posts title and thumbnale in loop
protected function render() {
$settings = $this->get_settings_for_display();
foreach ( $settings['show_elements'] as $element ) {
echo '<div>' . $element . '</div>';
}
}
protected function _content_template() {
?>
<# _.each( settings.show_elements, function( element ) { #>
<div>{{{ element }}}</div>
<# } ) #>
<?php
}
}
?> ```
2
Answers
First of all, you have to change
$settings['show_elements']
to
$settings['video_categories']
$element showes only the IDs of the selected categories.
With this WP-functions you get i.e. the Name of the Category by ID in the render output:
get_cat_name( int $cat_id )
For getting a post loop of the selected categories, you have to look further.
Maybe somebody can improve it or check it.
As I did you can create two functions, one you will pass to the select2 control type
as the ‘options’ parameter which will populate only the ‘value’ and the ‘text’ within
the options tags. The code will look something like this:
getControlTags() return an array with the options for the select2 field
like this value => name, the choosen values will be returned to pass them later into the render() method.
html returned by elementor in my case ‘slug}”>{tag->name}
And then in the render method you could pass some parameters to the second function which is
getTags($limit = 10, $include = []) limit = maximum number of tags to return and include = the specific tags to return
which in my case is by slug which I passed to the value attributes of the select2 options in the first add_control method
In my render function I check that if specicif tags are choosen then the ‘limit’ parameter will be the number of choosen tags.
If you do not set it to the number of choosen tags when specific tags are choosen then you may not see all the choosen tags when render.
At last I leave you the add_control method that shows you the text field to set the limit and
a switcher in case you also want to show the number of posts for that tag.
Hope this is helpfull for someone.
If you modify it a little with the right wp methods it will work for categories, posts, custom posts and custom taxonomies…
Sorry this is not the right exact function you wanted but I needed an explatation like this long time ago and I think
you (the reeder) could make it even more usefull for you.