admin管理员组文章数量:1290105
I'm having an issue with one of my meta box. I want the meta box displayed only on the default page template editing page.php
.
The meta box doesn't show up on the editing page.
add_action('add_meta_boxes', 'page_add_meta_boxes', 1);
function page_add_meta_boxes() {
global $post;
if(!empty($post)) {
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page.php') {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}
}
Thank you.
EDIT
Code updated following the comments of @SallyCJ
add_action('add_meta_boxes', 'page_add_meta_boxes', 10, 2);
function page_add_meta_boxes($post_type, $post) {
if(!empty($post)) {
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if(in_array( $pageTemplate, array( 'default', 'page.php' ) ) ) {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}
}
After checking with var_dump( $pageTemplate );
the result is:
"String(0) "" "
Note: If use a custom template page to display the metabox, it works.
I'm having an issue with one of my meta box. I want the meta box displayed only on the default page template editing page.php
.
The meta box doesn't show up on the editing page.
add_action('add_meta_boxes', 'page_add_meta_boxes', 1);
function page_add_meta_boxes() {
global $post;
if(!empty($post)) {
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page.php') {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}
}
Thank you.
EDIT
Code updated following the comments of @SallyCJ
add_action('add_meta_boxes', 'page_add_meta_boxes', 10, 2);
function page_add_meta_boxes($post_type, $post) {
if(!empty($post)) {
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if(in_array( $pageTemplate, array( 'default', 'page.php' ) ) ) {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}
}
After checking with var_dump( $pageTemplate );
the result is:
"String(0) "" "
Note: If use a custom template page to display the metabox, it works.
Share Improve this question edited Jul 17, 2021 at 15:32 Mathieu Préaud asked Jul 17, 2021 at 11:58 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges 1- 1 PS: I hope my answer helps, and if you haven't, you should check the tutorial on creating meta block - see developer.wordpress/block-editor/how-to-guides/metabox - and consider creating meta block instead. :) – Sally CJ Commented Jul 17, 2021 at 14:30
2 Answers
Reset to default 2If the "Default Template" option is selected from the Template dropdown, then WordPress will set the value of the _wp_page_template
meta to default
and not page.php
, so that's most likely the reason why your meta box is not showing/added.
So I would use if ( in_array( $pageTemplate, array( 'default', 'page.php' ) ) )
instead of if ( $pageTemplate == 'page.php' )
.
Additionally, I would also use the $post
variable that's available as the second parameter for the meta box function:
add_action('add_meta_boxes', 'page_add_meta_boxes', 10, 2); // require the 2nd parameter
function page_add_meta_boxes($post_type, $post) { // and define it in the function head
//global $post; // <- no need to do this
// your code
}
Update
If the meta value is empty (''
), then the template will be the default, e.g. page.php
for the page
post type.
So you could use if ( ! $pageTemplate || in_array( $pageTemplate, array( 'default', 'page.php' ) ) )
instead of the one I suggested above.
Or actually, you could use get_page_template_slug()
which is much easier. :) Example based on your updated code:
function page_add_meta_boxes($post_type, $post) {
$pageTemplate = get_page_template_slug( $post );
if(! $pageTemplate || 'page.php' === $pageTemplate) {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}
try this code i guess it should work correctly
add_action('add_meta_boxes', 'page_add_meta_boxes');
function page_add_meta_boxes(){
global $post;
if(!empty($post)):
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
$pageTemplate = ($pageTemplate == "page.php" || empty($pageTemplate) || $pageTemplate == " " || $pageTemplate == "default" )?true:false;
if($pageTemplate):
add_meta_box(
'page-repeatable-fields',
'Index',
'page_repeatable_meta_box_display',
'page',
'normal',
'low'
)
endif;
endif;
}
本文标签: phpShow meta box only for default page template
版权声明:本文标题:php - Show meta box only for default page template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741433682a2378508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论