492 lines
20 KiB
PHP
492 lines
20 KiB
PHP
<?php
|
|
use Elementor\Controls_Manager;
|
|
use Elementor\Group_Control_Text_Shadow;
|
|
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
|
|
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
|
|
use Elementor\Group_Control_Text_Stroke;
|
|
use Elementor\Group_Control_Typography;
|
|
|
|
class Elementor_Same_Posts_By_Category extends \Elementor\Widget_Base {
|
|
|
|
public function get_name() {
|
|
return 'same_posts_by_category';
|
|
}
|
|
|
|
public function get_title() {
|
|
return esc_html__( 'Same posts by category', 'elementor-addon' );
|
|
}
|
|
|
|
public function get_icon() {
|
|
return 'eicon-code';
|
|
}
|
|
|
|
public function get_categories() {
|
|
return [ 'basic' ];
|
|
}
|
|
|
|
public function get_keywords() {
|
|
return [ 'posts', 'posts by category', 'category' ];
|
|
}
|
|
|
|
protected function register_controls() {
|
|
$this->start_controls_section(
|
|
'section_title',
|
|
[
|
|
'label' => esc_html__( 'Columns', 'elementor' ),
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'posts_per_page',
|
|
[
|
|
'label' => esc_html__( 'Items Per Page', 'elementor-pro' ),
|
|
'type' => Controls_Manager::NUMBER,
|
|
'default' => 3,
|
|
'min' => 1,
|
|
]
|
|
);
|
|
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_title_style',
|
|
[
|
|
'label' => esc_html__( 'Box', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_title_box_margin',
|
|
[
|
|
'label' => esc_html__( 'Gap', 'elementor-pro' ),
|
|
'type' => Controls_Manager::SLIDER,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-categories-list .elementor-category-box:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_posts_image_style',
|
|
[
|
|
'label' => esc_html__( 'Image', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_image_style_width',
|
|
[
|
|
'label' => esc_html__( 'Width', 'elementor' ),
|
|
'type' => Controls_Manager::SLIDER,
|
|
'default' => [
|
|
'unit' => '%',
|
|
],
|
|
'tablet_default' => [
|
|
'unit' => '%',
|
|
],
|
|
'mobile_default' => [
|
|
'unit' => '%',
|
|
],
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'range' => [
|
|
'%' => [
|
|
'min' => 1,
|
|
'max' => 100,
|
|
],
|
|
'px' => [
|
|
'min' => 1,
|
|
'max' => 1000,
|
|
],
|
|
'vw' => [
|
|
'min' => 1,
|
|
'max' => 100,
|
|
],
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-img .elementor-category-box-img-link img' => 'width: {{SIZE}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_image_style_height',
|
|
[
|
|
'label' => esc_html__( 'Height', 'elementor' ),
|
|
'type' => Controls_Manager::SLIDER,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vh', 'custom' ],
|
|
'range' => [
|
|
'px' => [
|
|
'min' => 1,
|
|
'max' => 500,
|
|
],
|
|
'vh' => [
|
|
'min' => 1,
|
|
'max' => 100,
|
|
],
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-img .elementor-category-box-img-link img' => 'height: {{SIZE}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->add_responsive_control(
|
|
'section_posts_image_style_object_fit',
|
|
[
|
|
'label' => esc_html__( 'Object Fit', 'elementor' ),
|
|
'type' => Controls_Manager::SELECT,
|
|
'options' => [
|
|
'' => esc_html__( 'Default', 'elementor' ),
|
|
'fill' => esc_html__( 'Fill', 'elementor' ),
|
|
'cover' => esc_html__( 'Cover', 'elementor' ),
|
|
'contain' => esc_html__( 'Contain', 'elementor' ),
|
|
'scale-down' => esc_html__( 'Scale Down', 'elementor' ),
|
|
],
|
|
'default' => '',
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-img .elementor-category-box-img-link img' => 'object-fit: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->add_responsive_control(
|
|
'section_posts_image_style_object_position',
|
|
[
|
|
'label' => esc_html__( 'Object Position', 'elementor' ),
|
|
'type' => Controls_Manager::SELECT,
|
|
'options' => [
|
|
'center center' => esc_html__( 'Center Center', 'elementor' ),
|
|
'center left' => esc_html__( 'Center Left', 'elementor' ),
|
|
'center right' => esc_html__( 'Center Right', 'elementor' ),
|
|
'top center' => esc_html__( 'Top Center', 'elementor' ),
|
|
'top left' => esc_html__( 'Top Left', 'elementor' ),
|
|
'top right' => esc_html__( 'Top Right', 'elementor' ),
|
|
'bottom center' => esc_html__( 'Bottom Center', 'elementor' ),
|
|
'bottom left' => esc_html__( 'Bottom Left', 'elementor' ),
|
|
'bottom right' => esc_html__( 'Bottom Right', 'elementor' ),
|
|
],
|
|
'default' => 'center center',
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-img .elementor-category-box-img-link img' => 'object-position: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_image_style_margin',
|
|
[
|
|
'label' => esc_html__( 'Margin', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-img' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_posts_title_style',
|
|
[
|
|
'label' => esc_html__( 'Title', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'section_posts_title_style_color',
|
|
[
|
|
'label' => esc_html__( 'Text Color', 'elementor' ),
|
|
'type' => Controls_Manager::COLOR,
|
|
'global' => [
|
|
'default' => Global_Colors::COLOR_PRIMARY,
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-title .elementor-category-box-title-link' => 'color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_group_control(
|
|
Group_Control_Typography::get_type(),
|
|
[
|
|
'name' => 'section_posts_title_style_typography',
|
|
'global' => [
|
|
'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
|
|
],
|
|
'selector' => '{{WRAPPER}} .elementor-category-box .elementor-category-box-title .elementor-category-box-title-link',
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_title_style_margin',
|
|
[
|
|
'label' => esc_html__( 'Margin', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_posts_info_style',
|
|
[
|
|
'label' => esc_html__( 'Info', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'section_posts_info_style_color',
|
|
[
|
|
'label' => esc_html__( 'Text Color', 'elementor' ),
|
|
'type' => Controls_Manager::COLOR,
|
|
'global' => [
|
|
'default' => Global_Colors::COLOR_PRIMARY,
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-info' => 'color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_group_control(
|
|
Group_Control_Typography::get_type(),
|
|
[
|
|
'name' => 'section_posts_info_style_typography',
|
|
'global' => [
|
|
'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
|
|
],
|
|
'selector' => '{{WRAPPER}} .elementor-category-box .elementor-category-box-info',
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_info_style_margin',
|
|
[
|
|
'label' => esc_html__( 'Margin', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-info' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_posts_description_style',
|
|
[
|
|
'label' => esc_html__( 'Description', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'section_posts_description_style_color',
|
|
[
|
|
'label' => esc_html__( 'Text Color', 'elementor' ),
|
|
'type' => Controls_Manager::COLOR,
|
|
'global' => [
|
|
'default' => Global_Colors::COLOR_PRIMARY,
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-description' => 'color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_group_control(
|
|
Group_Control_Typography::get_type(),
|
|
[
|
|
'name' => 'section_posts_description_style_typography',
|
|
'global' => [
|
|
'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
|
|
],
|
|
'selector' => '{{WRAPPER}} .elementor-category-box .elementor-category-box-description',
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_description_style_margin',
|
|
[
|
|
'label' => esc_html__( 'Margin', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box .elementor-category-box-description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
$this->start_controls_section(
|
|
'section_posts_btn_style',
|
|
[
|
|
'label' => esc_html__( 'Button', 'elementor' ),
|
|
'tab' => Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'section_posts_btn_style_color',
|
|
[
|
|
'label' => esc_html__( 'Text Color', 'elementor' ),
|
|
'type' => Controls_Manager::COLOR,
|
|
'global' => [
|
|
'default' => Global_Colors::COLOR_PRIMARY,
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link' => 'color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_group_control(
|
|
Group_Control_Typography::get_type(),
|
|
[
|
|
'name' => 'section_posts_btn_style_typography',
|
|
'global' => [
|
|
'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
|
|
],
|
|
'selector' => '{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link',
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_btn_style_margin',
|
|
[
|
|
'label' => esc_html__( 'Margin', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_btn_style_padding',
|
|
[
|
|
'label' => esc_html__( 'Padding', 'elementor-pro' ),
|
|
'type' => Controls_Manager::DIMENSIONS,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_control(
|
|
'read_more_flex',
|
|
[
|
|
'label' => esc_html__( 'Position', 'elementor-pro' ),
|
|
'type' => Controls_Manager::CHOOSE,
|
|
'label_block' => false,
|
|
'options' => [
|
|
'left' => [
|
|
'title' => esc_html__( 'Left', 'elementor-pro' ),
|
|
'icon' => 'eicon-text-align-left',
|
|
],
|
|
'center' => [
|
|
'title' => esc_html__( 'Center', 'elementor-pro' ),
|
|
'icon' => 'eicon-text-align-center',
|
|
],
|
|
'right' => [
|
|
'title' => esc_html__( 'Right', 'elementor-pro' ),
|
|
'icon' => 'eicon-text-align-right',
|
|
],
|
|
],
|
|
'default' => 'left',
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link' => 'display: flex; align-items: center; justify-content: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
$this->add_responsive_control(
|
|
'section_posts_btn_style_gap',
|
|
[
|
|
'label' => esc_html__( 'Gap', 'elementor-pro' ),
|
|
'type' => Controls_Manager::SLIDER,
|
|
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .elementor-category-box-btn .elementor-category-box-btn-link img' => 'margin-left: {{SIZE}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
$this->end_controls_section();
|
|
}
|
|
|
|
protected function render() {
|
|
$settings = $this->get_settings();
|
|
$category = get_the_category();
|
|
|
|
$posts = get_posts(
|
|
array(
|
|
'category__in' => array_column( $category, 'term_id' ),
|
|
'post__not_in' => array( get_the_ID() ),
|
|
'posts_per_page' => $this->get_settings( 'posts_per_page' ),
|
|
)
|
|
);
|
|
|
|
if ( empty( $posts ) ) {
|
|
return;
|
|
}
|
|
|
|
?>
|
|
<div class="elementor-categories-list">
|
|
<?php foreach ( $posts as $post ) : ?>
|
|
<div class="elementor-category-box">
|
|
<div class="elementor-category-box-img">
|
|
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>" class="elementor-category-box-img-link">
|
|
<img src="<?php echo get_the_post_thumbnail_url( $post->ID, 'full' ); ?>" alt="">
|
|
</a>
|
|
</div>
|
|
|
|
<div class="elementor-category-box-info">
|
|
<?php echo $this->render_category(); ?>
|
|
</div>
|
|
|
|
<div class="elementor-category-box-title">
|
|
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>" class="elementor-category-box-title-link">
|
|
<?php echo esc_html( $post->post_title ); ?>
|
|
</a>
|
|
</div>
|
|
|
|
<div class="elementor-category-box-description">
|
|
<?php echo wp_trim_words( $post->post_content, 50 ); ?>
|
|
</div>
|
|
|
|
<?php echo $this->render_read_more($post); ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
protected function render_category() {
|
|
$categories = get_the_category();
|
|
|
|
if ( ! empty( $categories ) ) {
|
|
return $categories[0]->name;
|
|
}
|
|
}
|
|
|
|
protected function render_read_more($post) {
|
|
$settings = $this->get_settings();
|
|
$read_more_txt = $settings['read_more_text'];
|
|
$read_more_icon = $settings['read_more_icon'];
|
|
?>
|
|
|
|
<div class="elementor-category-box-btn">
|
|
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>" class="elementor-category-box-btn-link">
|
|
<?php echo $read_more_txt; ?>
|
|
<img src="<?php echo $read_more_icon['value']['url']; ?>" alt="">
|
|
</a>
|
|
</div>
|
|
|
|
<?php
|
|
}
|
|
}
|