記事毎に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に記述

ページトップへ移動