Skip to content

Instantly share code, notes, and snippets.

@marushu
Created August 22, 2014 18:46
Show Gist options
  • Save marushu/24cb10e23a1fda2299f2 to your computer and use it in GitHub Desktop.
Save marushu/24cb10e23a1fda2299f2 to your computer and use it in GitHub Desktop.
サイドバーに各種投稿の新着を出す。テキストウィジェットにショートコードOKな感じで
<?php
function update_list( $atts ) {
extract( shortcode_atts( array(
'post_type' => array( 'ranking', 'magazinenavi', 'topics' ),
'num' => 8,
'class' => 'recent_post',
'order' => 'DESC',
'orderby' => '',
'post_parent' => null,
), $atts ) );
global $post;
$args = array(
'post_type' => $post_type,
'numberposts' => $num,
'order' => $order,
'orderby' => $orderby,
'post_parent' => $post_parent,
);
$relational_posts = get_posts( $args );
setup_postdata( $post );
$html = '';
foreach( $relational_posts as $post ) {
$post_date = esc_attr( get_the_time( 'Y.m.d', $post->ID ) );
$post_link = esc_url( get_permalink( $post->ID ) );
$post_title = esc_attr( get_the_title( $post->ID ) );
$post_type = get_post_type( $post->ID );
$html .= "<li class='$post_type'><a href='$post_link'>$post_title</a><span class='post_date'>$post_date</span></li>";
}
wp_reset_postdata();
return "<ul class='$class'>$html</ul>";
}
add_shortcode( 'recent_update', 'update_list' );
// add widget area
add_filter('widget_text', 'do_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment