-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategory-post-shortcode.php
134 lines (122 loc) · 4.34 KB
/
category-post-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
<?php
/*
Plugin Name: category post shortcode
Plugin URI: http://ibnuyahya.com/wordpress-plugins/category-post-shortcode/
Description: To display post by category in your page/post
Author: ibnuyahya
Author URI: http://ibnuyahya.com/
Version: 2.4
Contributors
Ben McFadden - https://github.com/mcfadden
*/
/*
*
* How to use
* =============================================================================
* just put this shortcode in your post or pages
*
* [cat totalposts="3" category="1,3" thumbnail="true" excerpt="true" ]
*
* totalposts - your total number of post to display. default is -1
* category - category id. use comma , for multiple id
* thumbnail - set true if you want to display thumbnail. default is false
* thumbnail_type - set to 'meta' to use custom thumbnail-url meta field, set to 'featured_image' to use the featured image. default is 'meta'
* thumbnail_height - image size for the thumbnail. default is 130
* thumbnail_width - image size for the thumbnail. default is 130
* excerpt - set true if you want to display excertp. default is true
* date - set true if you want to display post date. default is false
* orderby - your post will order by . default post_date . check http://codex.wordpress.org/Template_Tags/get_posts for detail
* order - asc | desc
*
* thumbnail
* =============================================================================
* create custom field key as thumbnail-url and put your thumbnail url in the value area or set thumbnail_type to 'featured_image' and set a featured image
*
* style at your own
* =============================================================================
* you need to style your category-post-shortcode plugin in your style.css example
.cat-post{
width:100%;
}
.cat-post-list{
display: block;
margin-bottom: 20px;
position: relative;
}
.cat-post-images{
float:left;
width:140px;
display:block;
}
.cat-content{
width:350px;
float:right;
}
.cat-post-title{
display: block;
width:100%;
}
.cat-post-date
display: block;
width:100%;
}
.cat-clear{
clear:both;
}
*/
function cat_func($atts) {
extract(shortcode_atts(array(
'class_name' => 'cat-post',
'totalposts' => '-1',
'category' => '',
'thumbnail' => 'false',
'thumbnail_type' => 'meta',
'thumbnail_height' => '130',
'thumbnail_width' => '130',
'date' => 'false',
'excerpt' => 'true',
'orderby' => 'post_date',
'order' => 'desc'
), $atts));
$output = '<div class="'.$class_name.'">';
global $post;
$tmp_post = $post;
$myposts = get_posts("numberposts=$totalposts&category=$category&orderby=$orderby&order=$order");
foreach($myposts as $post) {
setup_postdata($post);
$output .= '<div class="cat-post-list">';
if($thumbnail == 'true') {
if ("featured_image" == $thumbnail_type){
$thumb_image = get_the_post_thumbnail($post->ID);
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $thumb_image, $matches);
$thumb_image = $matches [1] [0];
}else{
$thumb_image = get_post_meta($post->ID, 'thumbnail-url',true);
}
if(empty($thumb_image)){
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$thumb_image = $matches [1] [0];
}
if(empty($thumb_image)){
$thumb_image ='';
}
$output .= '<div class="cat-post-images"><img height="'.$thumbnail_height.'" width="'.$thumbnail_width.'" src="'.$thumb_image.'" /></div>';
}
$output .= '<div class="cat-content"><span class="cat-post-title"><a href="'.get_permalink().'">'.get_the_title().'</a></span>';
if ($date == 'true') {
$output .= '<span class="cat-post-date">'.get_the_date().'</span>';
}
if ($excerpt == 'true') {
$output .= '<span class="cat-post-excerpt">'.get_the_excerpt().'</span>';
}
$output .= '</div>
<div class="cat-clear"></div>
</div>';
};
$output .= '</div>';
$post = $tmp_post;
wp_reset_query();
return $output;
}
add_shortcode('cat', 'cat_func');
?>