get sub menu posts object by post id

piaoling  2013-03-28 10:43:01
/**
 * get sub menu posts object by post id
 * @param unknown_type $postId
 * @return multitype:NULL
 */
function getSubPostIdsByPostId($postId)
{
    $childPostIds = array();
    $subMenuIds = getSubMenus($postId);
    if (!empty($subMenuIds)) {
        foreach ($subMenuIds as $subId) {
            $childPostIds[] = getPostIdByMenuId($subId);
        }
    }
    return $childPostIds;
}

/**
 * Get sub menu by postId
 *
 *
 */
 function getSubMenus($postId)
 {
     global $wpdb;
     $parentMenuId = getMenuIdByPostId($postId);
     $query = "select post_id from {$wpdb->prefix}postmeta as m where meta_value = '$parentMenuId' and meta_key='_menu_item_menu_item_parent'";
     $subMenus = $wpdb->get_results($query);
     $subMenuIds = array();
     if (!empty($subMenus)) {
         foreach ($subMenus as $menu) {
             $subMenuIds[] = $menu->post_id;
         }
     }
     return $subMenuIds;
 }

 /**
  * Get menu Id by post id
  *
  *
  */
 function getMenuIdByPostId($postId){
     global $wpdb;
     $query = "select post_id from {$wpdb->prefix}postmeta as m where meta_value = '$postId' and meta_key='_menu_item_object_id'";
     $data = $wpdb->get_var($query);
     return $data;
 }
 
 /**
  * Get post Id by menu id
  *
  *
  */
 function getPostIdByMenuId($postId){
     global $wpdb;
     $query = "select meta_value from {$wpdb->prefix}postmeta as m where post_id = '$postId' and meta_key='_menu_item_object_id'";
     $data = $wpdb->get_var($query);
     return $data;
 }
类别 :  wordpress(8)  |  浏览(3004)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: