WordPress備忘録

記事毎にhead内に追加

//-----------------------------------
//記事別にhead内に追加
//-----------------------------------
add_action('admin_menu', 'add_head_hooks');
add_action('save_post', 'save_add_head');
add_action('wp_head','insert_add_head');
function add_head_hooks() {
	add_meta_box('add_head', 'headに追加', 'add_head_input', 'post', 'normal', 'high');
	add_meta_box('add_head', 'headに追加', 'add_head_input', 'page', 'normal', 'high');
}
function add_head_input() {
	global $post;
	echo '<input type="hidden" name="add_head_noncename" id="add_head_noncename" value="'.wp_create_nonce('add-head').'" />';
	echo '<textarea name="add_head" id="add_head" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_add_head',true).'</textarea>';
}
function save_add_head($post_id) {
	if (!wp_verify_nonce($_POST['add_head_noncename'], 'add-head')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$add_head = $_POST['add_head'];
	update_post_meta($post_id, '_add_head', $add_head);
}
function insert_add_head() {
	if (is_page() || is_single()) {
		if (have_posts()) : while (have_posts()) : the_post();
			echo get_post_meta(get_the_ID(), '_add_head', true);
		endwhile; endif;
		rewind_posts();
	}
}

上記をfunctions.phpに記述

CONTACT & ESTIMATE

まずはお気軽にご相談ください

ご相談・お見積もりは完全無料です。強引な営業は一切ございません。

相談・見積もり完全無料 最短即日〜翌営業日返信 全国オンライン対応

無料お見積もり・ご相談

最短1分で完了・24時間受付

完全無料・最短1分 フォームから無料相談する

LINE公式チャット相談

友だち追加で質問・概算費用もOK

友だち追加ですぐ質問 LINEで気軽に相談する

お電話での直接相談

平日 9:00〜17:00 / 代表が直接対応

タップでお電話発信 TEL. 0736-60-3822
電話 LINE 無料相談