Wordpress管理菜单指向主题文件

时间:2018-04-08 20:58:49

标签: php wordpress

我想将自定义菜单和子菜单添加到Wordpress管理区域。我在Codex中阅读了一些关于它的信息,我使用下面的函数添加了菜单,我将其放在我的主题 functions.php

add_action( 'admin_menu', 'restaurant_menu' );
function restaurant_menu() {
add_menu_page( 'Restaurants Admin Page', 'Restaurants', 'manage_options', get_template_directory() . '/inc/restaurants.php', 'restaurants_admin_page', 'dashicons-format-aside', 6  );

菜单已添加到Wordpress管理侧边栏,但是当我转到该页面时,它会转到下一个链接:

http://site.ge/wp-admin/admin.php?page=home%2Fbmtbow%2Fpublic_html%2Ftesting%2Fwp-content%2Fthemes%2Fmytheme%2Finc%2Frestaurants.php < / p>

但没有任何内容,例如它没有指向正确的PHP文件。

我做错了吗?你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

add_menu_page函数似乎不接受php文件作为内容的参数。但它确实需要一个函数名称。您可以使用函数中的内容调用您的php文件,然后将该函数作为参数传递给add_menu_page。 (作为一个侧面点,您可能会将参数的顺序混淆。slug应该是第4个而不是第5个。)有关add_menu_page()及其参数的更多信息可以找到here.

试试这个:

add_action( 'admin_menu', 'restaurant_menu' );
function restaurant_menu() {
    add_menu_page( 'Restaurants Admin Page', 'Restaurants', 'manage_options', 'restaurants_admin_page', 'restaurant_admin_page_contents', 'dashicons-format-aside', 6  );
}
function restaurant_admin_page_contents(){
    include get_template_directory() . '/inc/restaurants.php';
}