wordpress 代码获得菜单的方法

1、第一种,在function.php中添加这个东东,wp_nav_menu()主要是用这个函数

$menu = 'menu';
$nav_menu = wp_get_nav_menu_object($menu);
wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ));

然后访问任何页面就有了,但是呢这个页面不是特别好,很乱

wordpress 代码获得菜单的方法_第1张图片

2、我们用sql语句来查询这个东东

function show_category(){
    global $wpdb;
    $request = "SELECT $wpdb->terms.term_id, name FROM $wpdb->terms ";
    $request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ";
    $request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' ";
    $request .= " ORDER BY term_id asc";
    $categorys = $wpdb->get_results($request);
    foreach ($categorys as $category) { //调用菜单
        $output = ''.$category->name."(".$category->term_id.')';
        echo $output;
    }
}

在需要显示的地方添加下面的调用代码即可

这样就出来所有的了,我们可以自己加条件,比如利用term_taxonomy这个表的parent可以查出来这个子菜单有啥?

就像这样,我还顺便多查了个parent,但是呢,我现在是系统弄得列表页,我不知道怎么获得他的parent,还是不好查,555,继续学习中啊

function show_category(){
    global $wpdb;
    $request = "SELECT $wpdb->terms.term_id, name, parent FROM $wpdb->terms ";
    $request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ";
    $request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' and $wpdb->term_taxonomy.parent = 4 ";
    $request .= " ORDER BY term_id asc";
    $categorys = $wpdb->get_results($request);
    foreach ($categorys as $category) { //调用菜单
        $output = ''.$category->name."(".$category->term_id."(".$category->parent.'))';
        echo $output;
    }
}

 

转载于:https://www.cnblogs.com/xbxxf/p/8330062.html

你可能感兴趣的:(wordpress 代码获得菜单的方法)