PHP
[WordPress] 新着情報表示用ショートコード
2017年10月10日<?php/** * お知らせショートコード * 使用例: [topics_list count=10 cat_slug="news" ] */function oc_topics_list( $atts ) { $settings = shortcode_atts( array( 'cat_slug' => '', 'count' => 10, 'post_type' => 'post', ), $atts ); $html = ""; $args = array( 'posts_per_page' => $settings["count"], 'orderby' => 'date', 'order' => 'desc', 'post_type' => $settings["post_type"], 'post_status' => 'publish' ); if( !empty($settings["cat_slug"]) ) { $args["category_name"] = $settings["cat_slug"]; } $topics = get_posts( $args ); foreach($topics as $topic) { $date = get_the_time("Y年m月d日",$topic); $cats = get_the_category($topic->ID); $url = get_permalink($topic); $title = get_the_title($topic); $html .= "<li> <span class='date'>{$date}</span> <span class='content'><a href='{$url}'>{$title}</a></span> </li>"; } return "<ul class='topics-list'>{$html}</ul>";}add_shortcode( 'topics_list', 'oc_topics_list' );