Skip to content

Instantly share code, notes, and snippets.

@marushu
Last active December 20, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marushu/6087498 to your computer and use it in GitHub Desktop.
Save marushu/6087498 to your computer and use it in GitHub Desktop.
カテゴリ一覧用ショートコード
<?php
function category_list( $atts ) {
extract( shortcode_atts( array(
'post_type' => 'post',
'cat_name' => '',
'num' => 3,
'class' => 'relational_lists',
'orderby' => 'date',
), $atts ) );
global $post;
$args = array(
'post_type' => $post_type,
'category_name' => $cat_name,
'numberposts' => $num,
'orderby' => $order,
);
$relational_posts = get_posts( $args );
setup_postdata( $post );
$html = '';
foreach( $relational_posts as $post ) {
$post_date = get_the_time( 'Y年m月d日', $post->ID );
$post_link = get_permalink( $post->ID );
$post_title = get_the_title( $post->ID );
$html .= '<li><span>' . $post_date . '</span><a href="' . $post_link . '">' . $post_title . '</a></li>';
}
wp_reset_postdata();
return "<ul class='$class'>" . $html . "</ul>";
}
add_shortcode( 'cat_list', 'category_list' );
// add widget area
add_filter('widget_text', 'do_shortcode');
@marushu
Copy link
Author

marushu commented Jul 29, 2013

うへ…(^0^;)
orderとorderbyが。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment