If your custom page template filename is foobar.php
, you can use get_post_meta()
:
global $post;
if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
// The current page has the foobar template assigned
// do something
}
Personally, I like calling this inside my add_meta_boxes_page
callback, and wrapping it around the add_meta_box()
call itself.
function wpse82477_add_meta_boxes_page() {
global $post;
if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
add_meta_box( $args );
}
}
add_action( 'add_meta_boxes_page', 'wpse82477_add_meta_boxes_page' );
You’ll just need to instruct users to save the page after assigning the template, so that the meta box appears.
Related Posts:
- Custom field/meta populated by dropdown of existing posts?
- What is the index [0] for on post meta fields?
- Individual Widgets per Page
- Gutenberg add a custom metabox to default blocks
- Custom field metabox not showing in back-end
- How to hide meta box values from custom fields list?
- Arrange custom fields with drag and drop?
- How can I avoid re-inventing the Custom Fields wheel in my Plugin’s Metabox?
- Unable to get Preview of Uploaded image within a Custom Meta box
- Using media-upload.php to upload mp3 via custom fields
- How to store the value of a custom field dropdown select for post referencing?
- Only show metabox when date-value in other metabox is over?
- Custom Meta Boxes: Store two values in one repeatable field
- Problem in custom meta boxes
- Loop through two different sets of custom fields
- Multiplile values checkbox or select in custom meta box
- Create custom field key upon theme activation
- Adding a custom field or metabox to the post-thumbnail widget?
- Custom fields to save multiple values
- Undefined index error when saving content on metabox
- How to save multiple metaboxes?
- Metabox with multiple fields added by user and upload box
- My metabox class
- wp_handle_upload error “Specified file failed upload test” but still creates attachment?
- Woocommerce with metabox plugin
- wp_editor removes paragraph from custom meta box
- WP Custom Fields Metabox Disappears – ACF plugin issue [closed]
- Where did the Add New Custom Field go?
- How Meta Data is different from Custom Fields of POST/PAGE in WordPress
- Admin meta Image Path
- Tweak Meta for Post to work it for Pages also
- Odd PHP Code To Display HTML Of Meta Box [closed]
- how to fetch the meta field keys/meta boxes from a post type?
- How to Create Dynamic Fields in a Meta Box?
- Set front page option using custom fields?
- Can’t get metabox value (a url) returned using WPAlchemy
- Custom Meta Box (SELECT) Not Saving
- Custom Metabox additional item
- get_posts in meta box dropdown not showing latest posts
- Display Custom Meta Box Field Only If Value is Present
- get_post_meta() retrieves meta from a specific post on pages, not the page currently viewed
- How to pass page as an argument like post in WordPress?
- When creating a metabox do you have to create DB fields for the data?
- shortcode in a custom metabox
- Custom metabox not displaying multiselect data in edit mode
- How to validate select field in post meta?
- How to use media upload on metabox post page without breaking TinyMCE?
- How can I use custom properties to set different background on different pages?
- Let’s Create Custom Field Template Documentation
- Is it preferable to use custom code to create metaboxes instead of plugins (such as ACF) and if so why? [closed]
- Values entered in a meta box aren’t saved
- Scripts and tags will not save or output from my custom meta box
- Using WP Color Picker in Repeatable Fields
- MetaBox not saving after empty
- Displaying pages with a specific custom meta
- Can I save post meta programatically without setting metaboxes?
- How do I save Multiple fields in a meta box?
- Enable a short code in the custom meta field
- Display a different image for each page with editor
- How to validation for sanitize_URL?
- Meta box values are displayed on Custom Fields list. Is it possible to hide them?
- put saved metabox values back into fields and then display on the front end
- Custom metabox not working
- Meta Box Plugin Cloned Fields – Multiple Foreach values
- Show array of meta_value in Edit Post Coloum
- How to display data from custom fields I created?
- Display metabox title for custom fields with values
- Custom Field & Gravity Forms [closed]
- Working with multiple values and metaboxes
- Gutenberg add a custom metabox to default blocks
- Move the metaboxes to the very top of post editor
- How to improve my non-unique metadata MySQL entries?
- Add forms dynamically in admin pages?
- WordPress Blocks, setAttributes not saving
- non-unique #_ajax_nonce id in browser console
- add meta box considers the selected option as post parent
- Issue on Creating Checkbox Field On Custom Post type
- How do I display a custom field in an existing form on the front end?
- Why does this javascript search function in the WordPress Admin not work?
- get wordpress post loop by meta box date
- Create Meta boxes dynamically
- Sub Field of File Field
- Save input form on custom field
- Page template is missing fields or is invalid when I updating the template
- If metabox fields has content display content
- Get meta value when the page is a blog archive
- Can’t save meta field value if the title not set
- Display Data From This Custom Media Upload Meta Box?
- Why is wp_list_pages altering $post->ID of the page?
- Custom Field select list is truncated
- wp_postmeta are updated for only one page
- My meta boxes update wp_postmeta of posts but not of pages. What is wrong with the code?
- Meta Box Data added to header redirect
- Custom metaboxes not saving after switching themes
- I want my post to republish again after adding a custom field
- Get fields from metabox array
- Creating an If/Else statement using WPAlchemy MetaBox radio boxs
- Using CMB2 Plugin to create text_date metabox (datepicker) How do I save multiple dates? [closed]
- How do delete a meta key?
- Dynamic background image used in css after selector