Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi WordPress Experts please help me i want to get nav_menu_item_id and slug from wp database by given a page template like (store.php) this a page template and how we can get nave menu item id and slug that use this page template

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

Hi @Anjum:

I think the function get_nav_menu_id_by_page_template()is what you are looking for? You can place the code for this function in your theme's functions.php file, or in a .php for a plugin you are writing:

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;
}

Once you have the get_nav_menu_id_by_page_template() function in your code you can call it in your theme template file like so:

<?php $nav_menu_id = get_nav_menu_id_by_page_template('store.php'); ?>
share|improve this answer
    
I wondered if it would be faster to extend the nav menu query and let it collect the required information in an array which is accessible from the outside. +1 anyway. :) –  toscho Mar 28 '11 at 4:42
    
@MikeSchinkel i am getting an error with it Parse error: syntax error, unexpected T_SL –  Anjum Mar 28 '11 at 5:11
    
@Anjum - Didi you copy my code exactly? Do you accidentally indent or put a space in front of SQL;? –  MikeSchinkel Mar 28 '11 at 5:45
    
@MikeSchinkel i have copied and paste your code as it is in my function.php but i am getting this error any other way to.... –  Anjum Mar 28 '11 at 12:24
    
@Anjum - Can you give me the entire error message? File name, Line number and all? –  MikeSchinkel Mar 29 '11 at 7:16
show 7 more comments

Multiple pages may use the same template. There is no 1:1 relation between templates and pages or nav menu items, so you may get more than one item back.

Templates and item IDs are stored in the table postmeta. Look for _menu_item_object_id and _wp_page_template.

share|improve this answer
    
i am using special template (store.php) for my store (shop) page how can i get menu item id and slug by this template –  Anjum Mar 27 '11 at 11:26
add comment

function getIdFromMeta( $meta_key, $meta_value ) {
    global $wpdb;
    $pid = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$meta_value' AND meta_key = '$meta_key' ORDER BY post_id DESC") );
    if( $pid != '' )
        return $pid;
    else 
        return false;
}

it is work fine for me this solution give me Rochester Oliveira my question on stackoverflow Thanks Rochester Oliveira

this return post id but i needed nav menu id

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.