-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmz-wp-query.php
57 lines (46 loc) · 1.59 KB
/
kmz-wp-query.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
<?php
/**
* Plugin Name: KMZ Custom Loop with WP_Query class
* Description: Demonstrate how to customize the WordPress Loop WP_Query class
* Plugin URL: https://wpninja.pp.ua
* Author: Vladimir Kamuz
* Version: 1.0
*/
/**
* Custom Loop shortcode [wp_query_example cat="3, 4"]
*/
function custom_loop_pre_shortcode_wp_query($atts){
// define shortcode variable
extract(shortcode_atts(array(
'posts_per_page' => 5,
'orderby' => 'date',
'cat' => array(3, 4, 5, 8)
), $atts));
// define get_posts parameters
$args = array(
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'cat' => $cat
);
// query the posts
$posts = new WP_Query($args);
// begin output variable
$output = '<h3>' . esc_html__('Custom Loop Example: WP_Query', 'myplugin') . '</h3>';
if($posts->have_posts()){
while($posts->have_posts()): $posts->the_post();
$output .= '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
$output .= '<p>' . wp_trim_words(get_the_content(), 70, '...') . '</p>';
$output .= '<a class="btn btn-primary" href="' . get_permalink() . '">Read more...</a>';
$output .= '<hr>';
endwhile;
}
else{
$output .= '<div class="alert alert-danger">' . esc_html__('Sorry, no posts matched your criteria.', 'myplugin') . '</div>';
}
// reset post data
wp_reset_postdata();
// return output
return $output;
}
// register shortcode function
add_shortcode('wp_query_example', 'custom_loop_pre_shortcode_wp_query');