forked from dtbaker/elementor-custom-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-widget.php
93 lines (69 loc) · 2.2 KB
/
my-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Elementor;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Widget_My_Custom_Elementor_Thing extends Widget_Base {
public function get_name() {
return 'my-blog-posts';
}
public function get_title() {
return __( 'My Custom Widget', 'elementor-custom-element' );
}
public function get_icon() {
// Icon name from the Elementor font file, as per http://dtbaker.net/web-development/creating-your-own-custom-elementor-widgets/
return 'post-list';
}
protected function _register_controls() {
$this->start_controls_section(
'section_my_custom',
[
'label' => esc_html__( 'Blog Posts', 'elementor' ),
]
);
$this->add_control(
'some_text',
[
'label' => __( 'Text', 'elementor-custom-element' ),
'type' => Controls_Manager::TEXT,
'default' => '',
'title' => __( 'Enter some text', 'elementor-custom-element' ),
]
);
$this->add_control(
'posts_per_page',
[
'label' => __( 'Number of Posts', 'elementor-custom-element' ),
'type' => Controls_Manager::SELECT,
'default' => 5,
'options' => [
1 => __( 'One', 'elementor-custom-element' ),
2 => __( 'Two', 'elementor-custom-element' ),
5 => __( 'Five', 'elementor-custom-element' ),
10 => __( 'Ten', 'elementor-custom-element' ),
]
]
);
$this->end_controls_section();
}
protected function render( $instance = [] ) {
// get our input from the widget settings.
$custom_text = ! empty( $instance['some_text'] ) ? $instance['some_text'] : ' (no text was entered ) ';
$post_count = ! empty( $instance['posts_per_page'] ) ? (int)$instance['posts_per_page'] : 5;
?>
<h3>My Example Elementor Widget</h3>
<p>My text was: <?php echo esc_html( $custom_text );?> </p>
<h3>Some Recent Posts Here:</h3>
<ul>
<?php
$args = array( 'numberposts' => $post_count );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . esc_url( get_permalink( $recent["ID"] ) ). '">' . esc_html( $recent["post_title"] ).'</a> </li> ';
}
wp_reset_query();
?>
</ul>
<?php
}
protected function content_template() {}
public function render_plain_content( $instance = [] ) {}
}