Display Custom Field per Page Template


Hi Leah,

Here is an example how you would register post meta for a specific template file named example-template.php:

add_action( 'admin_init', 'example_register_post_meta' );
/**
 * Register example post meta
 */
function example_register_post_meta() {

 $fields = array(
    array(
      'id'         => 'example',
      'label'      => __( 'Example', 'example-domain' ),
      'type'       => 'text',
   ),
  );

  beans_register_post_meta( $fields, array( 'example-template.php' ), 'example_section_id', array(
    'title' => 'Example',
 ) );

}

Refering to the API documentation for the beans_register_post_meta() function, the second argument may be an array of post types ids, post ids or page template slugs. Note how we use example-template.php (change it with your template slug) in the example above which will display the registered post meta only when that template is selected.

Happy coding,


Super, thank you, I will give this a try!

Write a reply

Login or register to write a reply, it's free!