custom meta fields for specific page template in wordpress
/**
* Add About Us custom fields
*/
function add_about_meta_boxes() {
add_meta_box("about_content_meta", "About Content", "add_about_content_about_content_meta", "page", "normal", "low");
}
function add_about_content_about_content_meta()
{
global $post;
$custom = get_post_custom( $post->ID );
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'about-page-template.php' )
{
?>
<style>.width99 {width:99%;}</style>
<p>
<label>Our Mission:</label><br />
<textarea rows="5" name="our_mission" class="width99"><?= @$custom["our_mission"][0] ?></textarea>
</p>
<p>
<label>Our Vission:</label><br />
<textarea rows="5" name="our_vission" class="width99"><?= @$custom["our_vission"][0] ?></textarea>
</p>
<p>
<label>Our Goal:</label><br />
<textarea rows="5" name="our_goal" class="width99"><?= @$custom["our_goal"][0] ?></textarea>
</p>
<?php }
}
/**
* Save custom field data when creating/updating posts
*/
function save_about_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "our_mission", @$_POST["our_mission"]);
update_post_meta($post->ID, "our_vission", @$_POST["our_vission"]);
update_post_meta($post->ID, "our_goal", @$_POST["our_goal"]);
}
}
add_action( 'admin_init', 'add_about_meta_boxes' );
add_action( 'save_post', 'save_about_custom_fields' );
* Add About Us custom fields
*/
function add_about_meta_boxes() {
add_meta_box("about_content_meta", "About Content", "add_about_content_about_content_meta", "page", "normal", "low");
}
function add_about_content_about_content_meta()
{
global $post;
$custom = get_post_custom( $post->ID );
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'about-page-template.php' )
{
?>
<style>.width99 {width:99%;}</style>
<p>
<label>Our Mission:</label><br />
<textarea rows="5" name="our_mission" class="width99"><?= @$custom["our_mission"][0] ?></textarea>
</p>
<p>
<label>Our Vission:</label><br />
<textarea rows="5" name="our_vission" class="width99"><?= @$custom["our_vission"][0] ?></textarea>
</p>
<p>
<label>Our Goal:</label><br />
<textarea rows="5" name="our_goal" class="width99"><?= @$custom["our_goal"][0] ?></textarea>
</p>
<?php }
}
/**
* Save custom field data when creating/updating posts
*/
function save_about_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "our_mission", @$_POST["our_mission"]);
update_post_meta($post->ID, "our_vission", @$_POST["our_vission"]);
update_post_meta($post->ID, "our_goal", @$_POST["our_goal"]);
}
}
add_action( 'admin_init', 'add_about_meta_boxes' );
add_action( 'save_post', 'save_about_custom_fields' );
Comments
Post a Comment