-
Notifications
You must be signed in to change notification settings - Fork 1
/
iet_wp_query_shortcode.php
161 lines (126 loc) · 3.99 KB
/
iet_wp_query_shortcode.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
Plugin Name: IET WP_Query
Plugin URI: https://github.com/IET-OU/oer-evidence-hub-org
Description: Shortcode [wp_query] wrapper around WordPress core class: `WP_Query` [LACE]
Author: Nick Freear [@IET-OU]
Author URI: https://github.com/IET-OU
Version: 0.2
*/
/**
* @link https://gist.github.com/nfreear/e78c046784893c5639f3
* @link http://codex.wordpress.org/Class_Reference/WP_Query
* @copyright Nick Freear, 14 March 2015.
*/
class IET_WP_Query_Plugin {
const SHORTCODE = 'wp_query';
const ACTION = 'iet_wp_query_loop';
public function __construct() {
add_shortcode( self::SHORTCODE, array( &$this, 'shortcode' ));
}
public function shortcode( $attrs, $content = '', $name ) {
echo '<!--X-' . __CLASS__ . ': '. json_encode( $attrs ) . '-->';
$attrs = ('' === $attrs) ? array() : $attrs;
$query = null;
$format = 'full';
$post_type = 'post';
$post_status = 'publish';
$orderby = 'title'; #'date'
$order = 'ASC'; #'DESC'
$posts_per_page = -1;
$tag = null;
extract( $attrs );
$post_type = is_string( $post_type ) ? explode( ',', $post_type ) : $post_type;
$args = array(
'query ' => $query,
'post_type' => $post_type,
'post_status' => $post_status,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => $posts_per_page,
'tag' => $tag,
'x_format' => $format,
);
$classes = $this->get_classes( $args );
$args = is_string( $query ) ? $query : $args;
$the_query = new WP_Query( $args );
ob_start(); ?>
<!-- WP_Query SQL: <?php echo $the_query->request ?>; -->
<div class="<?php echo $classes ?>"><?php
// The Loop
if ('full' === $format):
$this->the_loop_full( $the_query );
elseif ('richlist' === $format):
$this->the_loop_richlist( $the_query );
elseif ('list' === $format):
$this->the_loop_list( $the_query );
else:
do_action( self::ACTION, $the_query, $format );
endif;
/* Restore original Post Data */
wp_reset_postdata();
?></div><?php
return ob_get_clean();
}
protected function the_loop_full( $the_query ) {
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post();
global $more; $more = 0;
get_template_part( 'content');
endwhile;
else: ?>
<p class=no-posts-found >No posts found.</p><?php
endif;
}
protected function the_loop_richlist( $the_query ) {
// The Loop
if ( $the_query->have_posts() ):
echo '<ul>';
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<li class="type-<?php echo get_post_type()?>"><a href="<?php the_permalink()?>"
><?php the_title()?></a> <i><?php the_author()?></i> <time datetime="<?php the_date_xml()?>"
><?php echo get_the_date()?></time> <small><?php echo get_post_type()?></small>
<?php
endwhile;
echo '</ul>';
else: ?>
<p class=no-posts-found >No posts found.</p><?php
endif;
}
protected function the_loop_list( $the_query ) {
// The Loop
if ( $the_query->have_posts() ):
echo '<ul>';
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php
endwhile;
echo '</ul>';
else: ?>
<p class=no-posts-found >No posts found.</p><?php
endif;
}
protected function get_classes( $attrs ) {
$classes[] = self::SHORTCODE; //'shortcode-'..
$classes[] = str_replace( '_', '-', strtolower( __CLASS__ ));
if ($attrs) {
foreach ($attrs as $key => $value) {
if (!$value) continue;
if (is_string( $value )) {
$classes[] = $key .'-'. $value;
} else {
//sort( $value, SORT_NATURAL );
sort( $value );
$classes[] = $key .'-'. implode( '-', $value );
}
}
}
return esc_attr(implode( ' ', $classes ));
}
}
$iet_wp_query = new IET_WP_Query_Plugin();
#End.