Tradycyjnie w functions.php jedziemy:
<?php
// Post list shortcodes
function wptuts_post_lists_sc( $atts, $content = null, $tag ) {
global $post;
$post_ID = $post->ID;
$post_author = $post->post_author;
extract( shortcode_atts( array(
'number' => 5,
'exclude_current' => 'yes',
'orderby' => 'date'
), $atts ) );
$args = '';
switch( $tag ) {
case "latest_posts":
// we don't need any arguments to retrieve latest posts :)
break;
case "category_posts":
$categories = get_the_category( $post_ID );
$first_category = $categories[0]->term_id;
$args = 'cat=' . $first_category;
break;
case "author_posts":
$args = 'author=' . $post_author;
break;
case "future_posts":
$args = 'post_status=future';
break;
}
$not_in = '&post__not_in[]=' . $post_ID;
if ( $exclude_current == 'no' )
$not_in = '';
$get_posts = get_posts( $args . $not_in . '&posts_per_page=' . $number . '&orderby=' . $orderby );
$output = '<ul class="' . $tag . '">';
foreach ( $get_posts as $post ) {
$output .= '<li><a href="' . get_permalink() . '" title="' . esc_attr( get_the_title() ) . '">' . get_the_title() . '</a></li>';
}
$output .= '</ul>';
wp_reset_query();
return $output;
}
add_shortcode( 'latest_posts', 'wptuts_post_lists_sc' );
add_shortcode( 'category_posts', 'wptuts_post_lists_sc' );
add_shortcode( 'author_posts', 'wptuts_post_lists_sc' );
add_shortcode( 'future_posts', 'wptuts_post_lists_sc' );
?>
Stworzyliśmy shortcode’y:
[latest_posts]
[category_posts]
[author_posts]
[future_posts]
Używamy je wraz z 3 wariacjami: cat, author, post_status