-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcs-shortcode.php
104 lines (82 loc) · 2.25 KB
/
wcs-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
<?php
/*** ShortCode ***/
class wcs_shortcode {
function __construct() {
}
function wcs_shortcode($att) {
$select_post = array();
$categorize = '';
// Select the post types
if(isset($att['post']) && !empty($att['post'])) {
foreach(explode(',', $att['post']) as $p) {
$select_post[] = $p;
}
} else {
$post_types = get_post_types( );
foreach($post_types as $p) {
$p = get_post_type_object($p);
if($p->name != 'nav_menu_item' && $p->name != 'revision') {
$select_post[] = $p->name;
}
}
}
// Check categorize
if(isset($att['categorize']) && ($att['categorize'] == 'on' || $att['categorize'] == 'off')) {
$categorize = $att['categorize'];
} else {
$categorize = 'on';
}
// check exclude
if(isset($att['exclude']) && !empty($att['exclude'])) {
$exclude = $att['exclude'];
} else {
$exclude = null;
}
// foreach post types
foreach($select_post as $p) {
$p = get_post_type_object($p);
echo '<h2>'.$p->labels->name.'</h2>';
if($categorize == "on") {
$categories = get_categories(array(
'type' => $p->name,
'taxonomy' => 'category'
));
foreach($categories as $c) {
$this->getLoop($p, $c, $exclude);
}
$this->getLoop($p, -1, $exclude);
} else {
$this->getLoop($p, null, $exclude);
}
}
}
function getLoop($p, $cat = null, $exclude = null) {
$args = array(
'post_type' => $p->name,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'menu_order ID',
'order' => 'DESC'
);
if(isset($exclude) && !is_null($exclude)) {
$args['exclude'] = $exclude;
}
if(!is_null($cat)) {
if($cat != -1)
$args['cat'] = $cat->term_id;
else
$args['cat'] = -1;
}
$myposts = get_posts($args);
if(count($myposts) >0) {
if(!is_null($cat) && $cat != -1) { // if Categorize == true
echo '<h3><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a></h3>';
}
echo '<ul>';
foreach($myposts as $post) {
echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
}
echo '</ul>';
}
}
}