你好@Anjum:
我认为功能get_nav_menu_id_by_page_template()
你在找什么?您可以将此函数的代码放置在主题的functions.php
文件,或在.php
对于您正在编写的插件:
function get_nav_menu_id_by_page_template($template) {
static $nav_menu_id;
if (!isset($nav_menu_id)) {
global $wpdb;
$sql = <<<SQL
SELECT
menu.post_id AS menu_id
FROM
{$wpdb->postmeta} AS template
INNER JOIN {$wpdb->postmeta} AS menu_id ON menu_id.meta_key=\'_menu_item_object_id\'
AND menu_id.meta_value=template.post_id
INNER JOIN {$wpdb->postmeta} AS menu ON menu_id.post_id=menu.post_id
WHERE 1=1
AND menu.meta_key=\'_menu_item_object\' AND menu.meta_value=\'page\'
AND template.meta_key=\'_wp_page_template\' AND template.meta_value=\'%s\'
SQL;
$nav_menu_id = $wpdb->get_var($wpdb->prepare($sql,$template));
}
return $nav_menu_id;
}
一旦你有了
get_nav_menu_id_by_page_template()
函数,您可以在主题模板文件中调用它,如下所示:
<?php $nav_menu_id = get_nav_menu_id_by_page_template(\'store.php\'); ?>