-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtermlist_shortcode.php
More file actions
64 lines (53 loc) · 1.64 KB
/
Copy pathtermlist_shortcode.php
File metadata and controls
64 lines (53 loc) · 1.64 KB
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
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Term list shortcode
* Description: List posts for a term with [termlist term='termslug' taxonomy='taxonomyslug']headline[/termlist].
* Plugin URI: http://marketpress.com/
* Version: 2013.12.05
* Author: MarketPress, Thomas Scholz
* Author URI: http://marketpress.com/
* Licence: GPL 2
* License URI: http://opensource.org/licenses/GPL-2.0
*/
add_shortcode( 'termlist', 'marketpress_termlist_shortcode' );
/**
* Handle the shortcode.
*
* @param array $atts
* @param string $content
* @return string
*/
function marketpress_termlist_shortcode( $atts, $content = '' ) {
if ( empty ( $atts[ 'term' ] ) )
return;
$default = array (
'taxonomy' => 'category',
'max' => 24,
'post_type' => 'post',
'post_status' => 'publish',
'term' => ''
);
$args = shortcode_atts( $default, $atts );
// Attachments have post status 'inherit' by default
if ( 'attachment' === $args[ 'post_type' ] && 'publish' === $args[ 'post_status' ] )
$args[ 'post_status' ] = 'inherit';
$posts = get_posts(
array (
'posts_per_page' => $args[ 'max' ],
'post_type' => $args[ 'post_type' ],
'post_status' => $args[ 'post_status' ],
$args[ 'taxonomy' ] => $args[ 'term' ]
)
);
if ( empty ( $posts ) )
return;
$list = array ();
foreach ( $posts as $post ) {
$list[] = sprintf(
'<li><a href="%1$s">%2$s</a></li>',
get_permalink( $post->ID ),
get_the_title( $post->ID )
);
}
return "<div class='marketpress_termlist'>$content<ul>" . join( '', $list ) . '</ul></div>';
}