-
Notifications
You must be signed in to change notification settings - Fork 0
/
site-sitemap.php
72 lines (53 loc) · 1.66 KB
/
site-sitemap.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
<?php
/*
Plugin Name: Site Sitemap
Plugin URI: http://joanartes.com
Description: Add a complete list of all posts in any page by [site-sitemap] shortcode. Atts: 'post_type', 'per_page'. Example: <code>[site-sitemap post_type="post, page" per_page="20"]</code>
Author: Joan Artés
Version: 1.0
Author URI: http://joanartes.com/
License: GPLv2 or later
*/
/**
* @todo
* 1. Exclude parameters
* 2. Documentation (xD)
*
*/
define( 'SS_VERSION', '1' );
function ss_shortcode( $atts ) {
extract( shortcode_atts( array(
'post_type' => 'post',
'per_page' => '10'
), $atts ) );
$post_types = explode(',', $post_type);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => $post_types,
'posts_per_page' => (int) $per_page,
'paged' => $paged
);
$sitemap = wp_cache_get( 'ss_site_sitemap' );
if ( $sitemap === false ) {
$sitemap = new WP_Query( $query_args );
wp_cache_set( 'ss_site_sitemap', $sitemap );
}
$html = '<ul>';
while ( $sitemap->have_posts() ) {
$sitemap->the_post();
$html .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$html .= '</ul>';
$html .= '<nav class="site-sitemap-nav">';
$html .= get_previous_posts_link( '« Previous', $sitemap->max_num_pages );
$html .= get_next_posts_link( 'Next »', $sitemap->max_num_pages);
$html .= '</nav>';
wp_reset_postdata();
return $html;
}
add_shortcode( 'site-sitemap', 'ss_shortcode' );
function ss_styles() {
wp_enqueue_style( 'site-sitemap-styles', plugin_dir_url( __FILE__ ) . "/css/site-sitemap.css", array(), SS_VERSION );
}
add_action( 'wp_enqueue_scripts', 'ss_styles' );
?>