Skip to content

Instantly share code, notes, and snippets.

@dragipostolovski
Last active April 1, 2021 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragipostolovski/a6bac596f80cdf01551ee89c66bcb780 to your computer and use it in GitHub Desktop.
Save dragipostolovski/a6bac596f80cdf01551ee89c66bcb780 to your computer and use it in GitHub Desktop.
<?php
use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Plugin;
use Elementor\Widget_Base;
if ( ! defined( 'ABSPATH' ) ) {
// Exit if accessed directly.
exit;
}
class Latest_Posts_Widget extends Widget_Base {
/**
* Get the widget's name.
*
* @return string
*/
public function get_name(): string {
return 'pe-latest-posts';
}
/**
* Get the widget's title.
*
* @return string
*/
public function get_title(): string {
return esc_html__( 'PE Latest Posts', PE_PLUGIN_DOMAIN );
}
/**
* Get the widget's icon.
*
* @return string
*/
public function get_icon(): string {
return 'fa fa-clipboard';
}
/**
* Add the widget to a category.
* Previously setup in the class-widgets.php file.
*
* @return string[]
*/
public function get_categories(): array {
return [ 'pe-category' ];
}
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => esc_html__( 'Latest Posts', PE_PLUGIN_DOMAIN ),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$this->add_group_control(
Group_Control_Background::get_type(),
[
'name' => 'background',
'label' => __( 'Background', PE_PLUGIN_DOMAIN ),
'types' => [ 'classic', 'gradient', 'video' ],
'selector' => '{{WRAPPER}} .pe-wrapper-latest-posts',
]
);
$this->add_control(
'pe-posts-order',
[
'label' => __( 'Order', PE_PLUGIN_DOMAIN ),
'type' => Controls_Manager::SELECT,
'multiple' => false,
'default' => 'DESC',
'options' => [
'ASC' => 'ASC',
'DESC' => 'DESC',
]
]
);
$this->add_control(
'pe-no-posts-message',
[
'label' => __( 'No post message', 'bco-core' ),
'type' => Controls_Manager::TEXT,
'default' => 'There are no posts at the moment.'
]
);
$this->end_controls_section();
}
protected function render() {
$order = $this->get_settings_for_display('pe-posts-order');
$message = $this->get_settings_for_display('pe-no-posts-message');
if (Plugin::$instance->editor->is_edit_mode()) {
// If the Elementor editor is opened.
}
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'orderby' => 'date',
'order' => $order,
);
$cpt = new WP_Query($args);
$posts = $cpt->posts;
if($posts) : ?>
<div class="pe-wrapper-latest-posts">
<?php foreach ($posts as $post ) { ?>
<div class="pe-latest-post">
<h1><?= $post->post_title; ?></h1>
</div>
<?php } ?>
</div>
<?php else: ?>
<div class="pe-wrapper-latest-posts">
<p><?= $message; ?></p>
</div>
<?php endif;
}
}
Plugin::instance()->widgets_manager->register_widget_type( new Latest_Posts_Widget() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment