Last active
December 20, 2015 06:39
-
-
Save marushu/6087498 to your computer and use it in GitHub Desktop.
カテゴリ一覧用ショートコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
うへ…(^0^;)
orderとorderbyが。。。