Raindrops1.439 カスタム投稿 テスト用プラグイン

コードをダブルクリックしていただくとコピーできます

<?php
/*
Plugin Name: Raindrops Custom Post Example
Plugin URI: https://www.tenman.info/wp3/manualraindrops/archives/3048
Description: このプラグインは、Raindrops1.438カスタム投稿試験用のプラグインです
Author: Tenman
Version: 1.0
Author URI: https://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 ショートコード投稿日のリンクに誤りがありましたので、削除 日付表示のみとしました


コメントは受け付けていません。