コードをダブルクリックしていただくとコピーできます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
<?php /* Plugin Name: Raindrops Custom Post Example Plugin URI: http://www.tenman.info/wp3/manualraindrops/archives/3048 Description: このプラグインは、Raindrops1.438カスタム投稿試験用のプラグインです Author: Tenman Version: 1.0 Author URI: http://www.tenman.info/ */ /** INDEX * * カスタム投稿タイプ カスタムタクソノミのセット * パーマリンクフラッシング * 投稿タイプ リスト用ショートコード * ショートコード用スタイル */ /** インストールの仕方 * * wp-conten/plugins ディレクトリに、 custom-post-type-example.php を作成して、このコードを貼り付けてください。 * プラグインページで Raindrops Custom Post Example を見つけたら、アクティベートしてください。 */ /** ショートコードの記述の仕方 * * Example * [rd_post_type type="product" count="3" date="true" title="タイトルを記述します"] * * type属性 product または newsが記述できます * count属性 表示件数を指定します * date属性 日付を表示する場合は true 表示しない場合は、 false または、属性を記述しない * title属性 タイトルが必要な場合は タイトルを記述 必要なければ属性を記述しない * * 記述する場所 * テキストウィジェット、投稿本文に記述 * 投稿本文に記述して、ピンナップ投稿ウィジェットでサイドバーに表示する事もできます。 * (投稿IDを入力、表示 デフォルト、投稿タイプ 投稿、content type 本文 でOK) */ add_action( 'init', 'raindrops_create_post_type' ); add_filter( 'widget_text', 'do_shortcode', 5 ); register_activation_hook( __FILE__, 'raindrops_plugin_activate' ); add_action( 'init', 'raindrops_plugin_flush_rewrite_rules_maybe', 20 ); register_deactivation_hook( __FILE__, 'raindrops_plugin_deactivate' ); add_shortcode( 'rd_post_type', 'raindrops_post_type_shortcode' ); add_action( 'wp_print_styles', 'raindrops_post_type_styles_method', 11 ); /** * カスタム投稿タイプ カスタムタクソノミのセット */ function raindrops_create_post_type() { register_post_type( 'product', array( 'labels' => array( 'name' => __( '商品一覧', 'raindrops' ), 'singular_name' => __( '商品', 'raindrops' ), 'add_new' => __( '商品を新規作成', 'raindrops' ), 'edit_item' => __( '商品を編集', 'raindrops' ), ), 'public' => true, 'has_archive' => true, ) ); add_post_type_support( 'product', array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author' ) ); register_post_type( 'news', array( 'labels' => array( 'name' => __( 'ニュース一覧' ), 'singular_name' => __( 'ニュース' ), 'add_new' => __( 'ニュースを新規作成' ), 'edit_item' => __( 'ニュースを編集' ), ), 'public' => true, 'has_archive' => true, ) ); add_post_type_support( 'news', array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author' ) ); $args = array( 'label' => 'ニュースカテゴリー', 'labels' => array( 'edit_item' => 'ニュースカテゴリーを編集', 'add_new_item' => '新規ニュースカテゴリーを追加', ), 'public' => true, 'hierarchical' => true ); register_taxonomy( 'news_cat', 'news', $args ); } /** * パーマリンクフラッシング */ function raindrops_plugin_activate() { if ( !get_option( 'myplugin_flush_rewrite_rules_flag' ) ) { add_option( 'myplugin_flush_rewrite_rules_flag', true ); } } function raindrops_plugin_flush_rewrite_rules_maybe() { if ( get_option( 'myplugin_flush_rewrite_rules_flag' ) ) { flush_rewrite_rules(); delete_option( 'myplugin_flush_rewrite_rules_flag' ); } } function raindrops_plugin_deactivate() { flush_rewrite_rules(); } /** * 投稿タイプ リスト用ショートコード */ function raindrops_post_type_shortcode( $atts ) { extract( shortcode_atts( array( 'type' => 'post', 'count' => 5, 'title' => '', 'date' => false, ), $atts ) ); if ( !empty( $title ) ) { $result = '<div class="rd-post-type-shortcode-wrapper">'; $result .= '<h3 class="rd-post-type-shortcode-title">' . $title . '</h3>'; } else { $result = ''; } $result .= '<ul class="rd-post-type-shortcode post-type-list-' . $type . '">'; $args = array( 'post_type' => $type, 'posts_per_page' => $count, 'post_status' => 'publish', ); $products = new WP_Query( $args ); if ( $products->have_posts() ) { while ( $products->have_posts() ) { $products->the_post(); if ( $date ) { $result .= '<li><span class="rd-post-type-shortcode-date">' . get_the_date() . '</span>'; } else { $result .= '<li>'; } if ( function_exists( 'raindrops_entry_title' ) ) { if( is_singular() ) { $result .= '<a href="' . get_permalink() . '">' . raindrops_entry_title( array( 'echo' => false ) ) . '</a></li>'; } else { $result .= raindrops_entry_title( array( 'echo' => false ) ) . '</li>'; } } else { $result .= '<a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>'; } } $result .= '</ul>'; if ( !empty( $title ) ) { $result .= '</div>'; } wp_reset_postdata(); return $result; } } /** * ショートコード用スタイル */ function raindrops_post_type_styles_method() { $custom_css = <<<CUSTOM_CSS .textwidget .rd-post-type-shortcode{ left:0; max-width:100%; } .textwidget .rd-post-type-shortcode li, .entry-content .rd-post-type-shortcode li{ list-style:none; margin-left:0; padding-left:0; margin-bottom:1em; border-bottom:1px solid rgba(127,127,127,.5); } .textwidget .rd-post-type-shortcode .h2-thumb, .textwidget .rd-post-type-shortcode .entry-title-text, .entry-content .rd-post-type-shortcode .entry-title-text, .entry-content .rd-post-type-shortcode .h2-thumb{ vertical-align:middle; } .rd-post-type-shortcode-date{ margin-right:1em; vertical-align:middle; display:block; margin-bottom:1em; } CUSTOM_CSS; wp_add_inline_style( 'style', $custom_css ); } |
2016 / 11 / 7 個別投稿ページで、ショートコードのリンクが無効になるバグを修正しました
2016 / 11 / 16 ショートコード投稿日のリンクに誤りがありましたので、削除 日付表示のみとしました