1 year ago
#386292
Chl Lila
How to display post from spesific custom post type's category on Elementor free?
I am trying to make an Elementor module for Custom Post Type, separate into 2 files.
[File 1] for setting control.
[File 2] to display the custom post type on the front page.
When I insert the Category directly to (File 2), everything works fine. The custom post type is displayed correctly. Can display post by category etc correctly.
But when I am trying to handle the category from the setting (File 2) it did not work. The post doesn't display.
How to collect the custom post type's category in Elementor? Note: my custom post type name: event, taxonomy: catevent, and the category sample: test.
Here is my code:
Code in admin setting
protected function register_controls() {
$terms = get_terms( array(
'taxonomy' =>'catevent',
'hide_empty' => true,
) );
$cat_names = array();
$cat_names = array('all');
foreach( $terms as $t ):
$cat_names[$t->term_id] = $t->name;
endforeach;
$this->add_control(
'cat_name',
[
'label' => __( 'From Category', 'module-for-elementor' ),
'type' => Controls_Manager::SELECT2 ,
'multiple' => true,
'default' => 'uncategorized',
'options' => $cat_names,
]
);
Code in the Loop
<?php $blog = array(
'catevent' => $settings['cat_name'],
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1,
'tax_query' => array( array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'NOT IN'
)),
);
?>
<div class="module1ol">
<?php $count = 0;
$the_query = new WP_Query( $blog );
if($the_query->have_posts()):
while($the_query->have_posts()):
$the_query->the_post();
?>
<div class="module1-post">
<div class="cat-color">
<a class="entry-title" href="<?php the_permalink();?>">
<?php esc_html_e(wp_trim_words( get_the_title(),8, ''), 'module-for-elementor'); ?>
</a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query();
endif;?>
</div>
Everything works fine when I insert the category directly like this but did not work when using code from (File 1).
<?php $blog = array(
'catevent' => 'test',
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1,
'tax_query' => array( array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'NOT IN'
)),
);
?>
Really appreciate any help anyone can provide.
wordpress
elementor
theming
0 Answers
Your Answer