投稿ページの追加/編集で[公開]ブロック内に新しいチェックボックスフィールドを追加したいとします。誰もそれをする方法を知っていますか?Wordpressの公開ボックス内の編集ポストページにフィールドを追加するには?
答えて
私は解決策をついに発見しました。私はそれが誰かのために良い使用になることを願っています。
add_action('post_submitbox_misc_actions', 'publish_in_frontpage');
function publish_in_frontpage($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
.'</span></div>';
}
function save_postdata($postid)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return false;
if (!current_user_can('edit_page', $postid)) return false;
if(empty($postid) || $_POST['post_type'] != 'article') return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
ニースが見つかりました。私はその箱に何かを置くことを決して考えていませんでしたが、そうした(いくらか非公開の)方法を持っているのはクールです。後で使用するためにこれをスター付きにしました! –
ほんの少しのアップデート: 'publish_in_frontpage()'にはパラメータがないようですので、内部で 'global $ post;'を使いました。 – frnhr
このコードは完全ではありませんが、それは機能しません。私は以下の固定/コメント付きコードを追加しました。 – user2019515
wordpressのAdvanced Custom Fieldsプラグインを使用してください。
高度なカスタムフィールドの** Publish **ブロック内にチェックボックスを追加する方法が見つかりません – rbncha
まあ!フィールドを追加する解決策が見つかりません公開ブロック。一時的な解決策として、以下のような簡単なコードを追加するだけで新しいブロックを追加しました。
add_action('admin_init', 'category_metabox');
//add new publish to frontpage box
add_meta_box(
'publish_in_frontpage',
'Publish in Frontpage',
'publish_in_frontpage_callback',
'article',
'side',
'high'
);
function publish_in_frontpage_callback($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>';
}
add_action('save_post', 'save_postdata');
function save_postdata($postid)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return false;
if (!current_user_can('edit_page', $postid)) return false;
if(empty($postid) || $_POST['post_type'] != 'article') return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
rbnchaのコードがそのまま動作と微調整の多くを必要としませんでした、次のコードは、私が思い付いたものです。私はすべてを完全に説明するいくつかのコメントを追加しました。
次のコードでは、投稿の公開ブロックにチェックボックスを追加し(投稿タイプを簡単に変更できます)、データベースの値を保存/取得します。マイナーチェンジをすると、テキストフィールドや好きなものを簡単に追加できます。
あなたのテーマやプラグインにはmy_
をユニークキーに変更する必要があります。
add_action('post_submitbox_misc_actions', 'my_featured_post_field');
function my_featured_post_field()
{
global $post;
/* check if this is a post, if not then we won't add the custom field */
/* change this post type to any type you want to add the custom field to */
if (get_post_type($post) != 'post') return false;
/* get the value corrent value of the custom field */
$value = get_post_meta($post->ID, 'my_featured_post_field', true);
?>
<div class="misc-pub-section">
<?php //if there is a value (1), check the checkbox ?>
<label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
</div>
<?php
}
add_action('save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
/* check if this is an autosave */
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return false;
/* check if the user can edit this page */
if (!current_user_can('edit_page', $postid)) return false;
/* check if there's a post id and check if this is a post */
/* make sure this is the same post type as above */
if(empty($postid) || $_POST['post_type'] != 'post') return false;
/* if you are going to use text fields, then you should change the part below */
/* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */
/* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
if(isset($_POST['my_featured_post_field'])){
/* store the value in the database */
add_post_meta($postid, 'my_featured_post_field', 1, true);
}
else{
/* not marked? delete the value in the database */
delete_post_meta($postid, 'my_featured_post_field');
}
}
あなたはカスタムフィールドについての詳細を読みたい場合はこちらをご覧ください:http://codex.wordpress.org/Custom_Fields
'カスタムフィールドが送信されたかどうかをチェックする ' チェックボックスの前に、同じ名前と値= 0で非表示フィールドを追加できます。 – gaRex
これは悪い習慣のようですか?ハッキーな解決策? – user2019515
- 多くのフレームワークがそれをしません。だから、これは既存の練習と名付けることができます。 – gaRex
- 1. WordPress:カスタムポストタイプの追加/編集ページの必須フィールド
- 2. スクロールバーを編集ボックスに追加する手助けが必要
- 3. jqgridの追加/編集フォームに列の2つの編集フィールドを表示
- 4. WordPress:カスタム分類にチェックボックスを追加するフォームを追加/編集する
- 5. テクニカルマニュアル編集/公開/コラボレーションオンライン
- 6. ポップアップ画面に編集フィールドを追加する
- 7. Wordpress、ホームページの編集を編集する
- 8. WordPressのテーマコードを編集するには?
- 9. QPushButton - ライン編集ボックスにテキストの文字列をプッシュボタンで追加する方法
- 10. の追加/編集値は
- 11. Wordpressのポストページ番号
- 12. Wordpress - div内のテキストをクリエイティブ用に編集可能にする
- 13. Wordpress、ポストページにリンクを挿入/編集するためのフックがありますか?
- 14. 編集ボックス内の大文字
- 15. は、フォームフィールドにカスタムコントロール内の編集ボックスのバインドプログラム的
- 16. Odoo - one2manyフィールドの追加または編集の計算
- 17. X11の編集ボックス
- 18. 編集/追加/編集をするためにどのようにデータグリッド
- 19. URLに編集テキストの値を追加
- 20. Wordpressのdivボックスへのリンクを追加
- 21. 公開アプリケーションに.pdfを追加する
- 22. EditText編集ボックス
- 23. Wordpressのコメント欄にフィールドを追加
- 24. iTextでXFA PDFを編集する(ノード内のフィールドのみを編集する)
- 25. wordpressのウィジェットオプションにフィールドを追加するには?
- 26. CheckedListBox編集/追加
- 27. WooCommerce:ベンダーカテゴリの追加/編集
- 28. ポストタイプのリストに公開ボタンとプレビューボタンを追加するには?
- 29. 次のスクリプトを編集、削除、追加するには?
- 30. 私のゲームの編集ボックス?
はこれを確認してください - > http://wordpress.org/extend/plugins/more-fields/ – Rikesh
私はより多くのを確認していますフィールド。 add_meta_box()のような別の方法で新しいフィールドを追加することはできませんか? – rbncha