/
theme-posts-shortcode.php
123 lines (97 loc) · 2.49 KB
/
theme-posts-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
<?php
/**
* Plugin Name: Theme Posts Shortcode
* Plugin URI: http://github.com/joemcgill/theme-posts-shortcode
* Description: Include a set of posts on a page
* Version: 1.0.0
* Author: Joe McGill
* Author URI: http://joemcgill.net
* License: GPLv2 or later
*/
// Exit early if this file is called directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers a shortcode that can be used to display a list of posts on any page.
*/
Class Theme_Posts_Shortcode {
/**
* Placeholder method
*
* @since 1.0.0
*/
public function __construct() { }
/**
* Return a singleton instance of the class and call setup functions.
*
* @since 1.0.0
* @return Theme_Posts_Shortcode
*/
public static function factory() {
static $instance = false;
if ( ! $instance ) {
$instance = new self();
$instance->setup();
}
return $instance;
}
/**
* Register settings, add filters, etc.
*
* @since 1.0.0
*/
public function setup() {
add_shortcode( 'show-posts', array( $this, 'theme_posts_shortcode' ) );
}
/**
* Shortcode functionality to show posts.
*
* @since 1.0.0
*/
function theme_posts_shortcode( $atts ) {
$output = false;
// Pull in shortcode attributes and set defaults
$defaults = array(
'category' => '',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => '10',
'tag' => '',
);
// Process shortcode attribtues
$atts = shortcode_atts( $defaults, $atts, 'show-posts' );
// Set up initial query for post
$args = array(
'category_name' => $atts['category'],
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'posts_per_page' => $atts['posts_per_page'],
'tag' => $atts['tag'],
'ignore_sticky_posts' => 1,
'no_found_rows' => true,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
global $wp_query;
// Backup the main query.
$saved_query = $wp_query;
// Replace the main query by reference.
$wp_query = $query;
// Capture the output so we can return it to the shortcode process.
ob_start();
echo '<div class="show-posts-wrapper">';
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
get_template_part( 'content', get_post_format() );
}
echo '</div>';
$output = ob_get_clean();
// Clean up after ourselves.
$wp_query = $saved_query;
wp_reset_postdata();
}
return $output;
}
}
Theme_Posts_Shortcode::factory();