ショートコードの自動表示
-
カスタムフィールドの
キーに「shortcode」
値に[]の中身
を入れるとして、functions.php:
function my_append_shortcodes($content){ $shortcodes = get_post_meta(get_the_ID(), 'shortcode'); foreach($shortcodes as $shortcode) $content .= '[' . $shortcode . ']' . "\n"; return $content; } add_filter('the_content', 'my_append_shortcodes', 1);
で OK です。
カスタムフィールドの値にカッコ付きでショートコードを書きたいなら
$content .= $shortcode . "\n";
とします。kzさん、いつもありがとうございます。
カスタムフィールドにshortcode、値にgalleryと入れますと確かに意図した通りになるのですが、私の説明不足でした、galleryの入力そのものを実は省きたいのです。
functions.phpでgalleryなどいくつかのshortcodeを記入しておき、カスタムフィールドにある名前のプルダウンメニューからそれらのいずれかを選択し、値は何も入れずに終了。
といったことが希望なのですが、このような事可能でしょうか。お手数をおかけします。
あらかじめ名前のgalleryを選択するだけで投稿本文内に
[gallery]
の記入位置は投稿本文内で変動でしょうか?
変動ならIMEを使うってのがアナログ的で便利かと。
(マウスを移動&セレクトする時間を考えたらキーボードで手っ取り早く、、、シュートコードの数にもよりますが)固定位置ならif文でも可能ですか?
第三者(ブログアカウントユーザー)も含め、統一化したいなら投稿欄のツールをカスタマイズしてシュートコードを複数選択する方法もあるのかな?
使用方法の要件定義によってお答えできるスキルが変わってくると思います。
rockhopper2000さん、ご指摘ありがとうございます。
固定位置ではなく、第三者を前提としていますので、ショートコードをプルダウンから選択を希望しております。galleryとかその他プラグインのショートコードなどを2-3個プルダウンには入れたいと思っています。
お手数をおかけしますが、よろしくお願いします。
やはり第三者でしたか。
正直に「投稿欄のツールをカスタマイズ」を含め私のスキルでは結構難しいです。
たとえば、「Get Custom Field Values」プラグインのBefore text: などの使用で実現できないのかな?プラグインのカスタマイズならコアがVer.UPしても面倒なことにはなりそうもないでしょうし。
(後記:上のプラグインを調べてみましたがちょっと無理みたいです。)
何か考えてみますが、、、。WordPressマスターの方、どなたかアイデアをお願いします。
functions.php:
$my_shortcodes = array('none', 'gallery', 'gallery link="file"', 'gallery order="DESC"'); function my_meta_shortcode_box(){ add_meta_box('my_meta_shortcode_box', 'ショートコードの指定', 'my_meta_shortcode_html', 'post', 'normal', 'high'); } function my_meta_shortcode_html($post, $box){ $value = get_post_meta($post->ID, 'my_meta_shortcode', true); if(!$value) $value = 0; echo '<input type="hidden" name="my_meta_shortcode_nonce" id="my_meta_shortcode_nonce" value="'.wp_create_nonce(get_bloginfo('template_url') . 'my_meta_shortcode').'" />' . "\n" . '<div><label for="my_meta_shortcode">ショートコード</label>' . "\n" . '<select id="my_meta_shortcode" name="my_meta_shortcode">' . "\n"; global $my_shortcodes; foreach($my_shortcodes as $key => $my_shortcode) echo '<option ' . ($value == $key ? 'selected="selected" ' : '') . 'value="'. $key .'">[' . $my_shortcode . ']</option>' . "\n"; echo '</select></div>'; } function my_meta_shortcode_update($post_id){ if(!wp_verify_nonce( $_POST['my_meta_shortcode_nonce'], get_bloginfo('template_url') . 'my_meta_shortcode')){ return $post_id; } if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; if('page' == $_POST['post_type']){ if(!current_user_can('edit_page', $post_id)) return $post_id; }else{ if(!current_user_can('edit_post', $post_id)) return $post_id; } if(isset($_POST['my_meta_shortcode']) && $_POST['my_meta_shortcode'] > 0) update_metadata('post', $post_id, 'my_meta_shortcode', $_POST['my_meta_shortcode']); else delete_metadata('post', $post_id, 'my_meta_shortcode', $_POST['my_meta_shortcode']); } add_action('admin_menu', 'my_meta_shortcode_box'); add_action('save_post', 'my_meta_shortcode_update'); function my_append_shortcodes($content){ global $my_shortcodes; $shortcodes = get_post_meta(get_the_ID(), 'my_meta_shortcode'); foreach($shortcodes as $shortcode) $content .= '[' . $my_shortcodes[$shortcode] . ']' . "\n"; return $content; } add_filter('the_content', 'my_append_shortcodes', 1);
マークアップその他諸々はお好みでアレンジしてください。
- トピック「ショートコードの自動表示」には新たに返信することはできません。