ACF 选项添加带有回调函数的子菜单
ACF options add submenu with a callback function
我想将自定义子菜单添加到选项页面,以便我可以使用我添加的回调函数呈现页面。如果我创建 acf_add_options_sub_page 我必须使用 acf 字段来生成选项页面。
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme General Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Header Settings',
'menu_title' => 'Header',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Footer Settings',
'menu_title' => 'Footer',
'parent_slug' => 'theme-general-settings',
));
}
我试过的
add_action( 'admin_menu', 'main_home' );
/**
* Adds a submenu page under a custom post type parent.
*/
function main_home() {
add_submenu_page(
'theme-general-settings',
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-shortcode-ref',
'books_ref_page_callback'
);
}
/**
* Display callback for the submenu page.
*/
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
结果
它不起作用:url 变得像:网站。com/wp-admin/books-shortcode-ref
如果我将 books-shortcode-ref 更改为 theme-general-settings 它可以工作,但它变得与过去使用的 acf 插件相同..我必须使用 acf 字段添加选项
请帮忙
这里是我刚刚用于将自定义子选项页面添加到适用于您的示例的 ACF 选项页面的代码。你必须用相同的标识符声明你的选项页面两次(在 ACF 中和正常方式)。所以,这有点棘手,但它有效:
function add_acf_option_page() {
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme General Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-general-settings',
'capability' => 'manage_options',
'redirect' => false
));
acf_add_options_sub_page( array(
'page_title' => __( 'Books Shortcode Reference', 'textdomain' ),
'menu_title' => __( 'Shortcode Reference', 'textdomain' ),
'parent_slug' => 'theme-general-settings',
'capability' => 'manage_options',
'menu_slug' => 'books-ref-page',
) );
}
}
add_action('acf/init', 'add_acf_option_page' );
function add_custom_option_page() {
add_submenu_page(
null,
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-ref-page',
'books_ref_page_callback'
}
add_action('admin_menu', 'add_custom_option_page');
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
希望对您有所帮助!
您也不必同时执行 parent 和 child。我希望一个页面成为另一个现有项目(自定义 post 类型)的子页面,所以我只是做了下面的事情。
要将您的页面设置为现有项目的 child/sub(甚至不需要是选项页面,我的也是 CPT)只需在 acf_add_options_page
'parent_slug' => 'edit.php?post_type=orders',
这是全部代码
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'ACF Options',
'menu_title' => 'ACF Options',
'parent_slug' => 'edit.php?post_type=orders',
'menu_slug' => 'books-ref-page',
'redirect' => false
));
}
function add_custom_option_page() {
add_submenu_page(
'edit.php?post_type=orders',
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-ref-page',
'books_ref_page_callback'
);
} add_action('admin_menu', 'add_custom_option_page');
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
我不喜欢@Luc 的回答是,当您单击子菜单 link 时,管理菜单中的“主”link 没有获得 .wp-menu-open
class 通常会为每个子 link.
打开菜单
这是完成它的另一种选择。
<?php
add_action( 'acf/init',
/**
* Register the option page as a `root` admin menu item
*/
function () {
// create a root menu
add_menu_page( 'Foo', 'Foo', 'edit_posts', 'foo', null, 'dashicons-images-alt' );
// use the same slug for the options page
acf_add_options_page( [
'title' => 'Foo',
'menu_slug' => 'foo',
'parent_slug' => 'foo',
] );
}
);
add_action( 'admin_menu',
/**
* Register the custom submenu with its callback function.
* The hook priority must be over 99 which is used by ACF to register the admin menu.
*/
function () {
// remove the subpage created by ACF from the menu
remove_submenu_page( 'foo', 'foo' );
// manually add the submenu page linked to ACF options page
add_submenu_page( 'foo', 'Foo', 'Foo', 'edit_posts', 'foo', null );
// add the custom subpage with callback
add_submenu_page( 'foo', 'Bar sub menu', 'Bar sub menu', 'edit_posts', 'foo-import', 'foo_bar_callback' );
}, 100
);
function foo_bar_callback() {
?>
<h1>Bar sub menu</h1>
<div>Some stuff</div>
<?php
}
我想将自定义子菜单添加到选项页面,以便我可以使用我添加的回调函数呈现页面。如果我创建 acf_add_options_sub_page 我必须使用 acf 字段来生成选项页面。
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme General Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Header Settings',
'menu_title' => 'Header',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Footer Settings',
'menu_title' => 'Footer',
'parent_slug' => 'theme-general-settings',
));
}
我试过的
add_action( 'admin_menu', 'main_home' );
/**
* Adds a submenu page under a custom post type parent.
*/
function main_home() {
add_submenu_page(
'theme-general-settings',
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-shortcode-ref',
'books_ref_page_callback'
);
}
/**
* Display callback for the submenu page.
*/
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
结果 它不起作用:url 变得像:网站。com/wp-admin/books-shortcode-ref
如果我将 books-shortcode-ref 更改为 theme-general-settings 它可以工作,但它变得与过去使用的 acf 插件相同..我必须使用 acf 字段添加选项
请帮忙
这里是我刚刚用于将自定义子选项页面添加到适用于您的示例的 ACF 选项页面的代码。你必须用相同的标识符声明你的选项页面两次(在 ACF 中和正常方式)。所以,这有点棘手,但它有效:
function add_acf_option_page() {
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme General Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-general-settings',
'capability' => 'manage_options',
'redirect' => false
));
acf_add_options_sub_page( array(
'page_title' => __( 'Books Shortcode Reference', 'textdomain' ),
'menu_title' => __( 'Shortcode Reference', 'textdomain' ),
'parent_slug' => 'theme-general-settings',
'capability' => 'manage_options',
'menu_slug' => 'books-ref-page',
) );
}
}
add_action('acf/init', 'add_acf_option_page' );
function add_custom_option_page() {
add_submenu_page(
null,
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-ref-page',
'books_ref_page_callback'
}
add_action('admin_menu', 'add_custom_option_page');
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
希望对您有所帮助!
您也不必同时执行 parent 和 child。我希望一个页面成为另一个现有项目(自定义 post 类型)的子页面,所以我只是做了下面的事情。
要将您的页面设置为现有项目的 child/sub(甚至不需要是选项页面,我的也是 CPT)只需在 acf_add_options_page
'parent_slug' => 'edit.php?post_type=orders',
这是全部代码
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'ACF Options',
'menu_title' => 'ACF Options',
'parent_slug' => 'edit.php?post_type=orders',
'menu_slug' => 'books-ref-page',
'redirect' => false
));
}
function add_custom_option_page() {
add_submenu_page(
'edit.php?post_type=orders',
__( 'Books Shortcode Reference', 'textdomain' ),
__( 'Shortcode Reference', 'textdomain' ),
'manage_options',
'books-ref-page',
'books_ref_page_callback'
);
} add_action('admin_menu', 'add_custom_option_page');
function books_ref_page_callback() {
?>
<div class="wrap">
<h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
<p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
</div>
<?php
}
我不喜欢@Luc 的回答是,当您单击子菜单 link 时,管理菜单中的“主”link 没有获得 .wp-menu-open
class 通常会为每个子 link.
这是完成它的另一种选择。
<?php
add_action( 'acf/init',
/**
* Register the option page as a `root` admin menu item
*/
function () {
// create a root menu
add_menu_page( 'Foo', 'Foo', 'edit_posts', 'foo', null, 'dashicons-images-alt' );
// use the same slug for the options page
acf_add_options_page( [
'title' => 'Foo',
'menu_slug' => 'foo',
'parent_slug' => 'foo',
] );
}
);
add_action( 'admin_menu',
/**
* Register the custom submenu with its callback function.
* The hook priority must be over 99 which is used by ACF to register the admin menu.
*/
function () {
// remove the subpage created by ACF from the menu
remove_submenu_page( 'foo', 'foo' );
// manually add the submenu page linked to ACF options page
add_submenu_page( 'foo', 'Foo', 'Foo', 'edit_posts', 'foo', null );
// add the custom subpage with callback
add_submenu_page( 'foo', 'Bar sub menu', 'Bar sub menu', 'edit_posts', 'foo-import', 'foo_bar_callback' );
}, 100
);
function foo_bar_callback() {
?>
<h1>Bar sub menu</h1>
<div>Some stuff</div>
<?php
}