如何在Wordpress中获取当前选定的菜单项

时间:2015-10-03 22:43:09

标签: php wordpress

请告诉我如何获取//读取菜单中当前所选项目的父级.... Wordpress // PHP

示例:

菜单结构:

一个 -AA -ab -ac

乙 -ba -BB -bc

因此,如果用户选择,请说" ba"项目然后我需要得到" B" item(...)

谢谢

3 个答案:

答案 0 :(得分:0)

你所说的是面包屑。这应该有所帮助:https://wordpress.org/support/topic/display-page-parent-on-page-with-title

基本上:$post->post_parent会为您提供父页面的ID。

答案 1 :(得分:0)

如果您要获取当前页面的父页面,可以使用Get Post Parent Id,也可以使用祖先https://codex.wordpress.org/Function_Reference/get_post_ancestors

答案 2 :(得分:0)

感谢大家提供的提示和指南。

我已经用这种方式解决了这个问题:

$current_page_id = $post->ID;
$page_children = fnGetPageChildren($current_page_id);

class currentpage_child {
    var $title;var $link;
        function set_data ($page_title,$page_link){
            $this->title = $page_title;
            $this->link = $page_link;
        }
}

function fnGetPageChildren ($page_id){
                    // Set up the objects needed
                    $my_wp_query = new WP_Query();
                    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

                    //get current page parrent id
                    $parent_id = wp_get_post_parent_id($page_id);
                    //get current page parent title
                    $parent_title = get_the_title($parent_id);

                    // Get the page as an Object
                    $obj_get_help =  get_page_by_title($parent_title);

                    // Filter through all pages and find Portfolio's children
                    $get_help_children = get_page_children( $obj_get_help->ID, $all_wp_pages);

                    $childrenpages_count = 0;
                        foreach ($get_help_children as $obj) {
                            $arr_currentpage_children[$childrenpages_count] = new currentpage_child();
                            $arr_currentpage_children[$childrenpages_count] -> set_data ($obj->post_title,$obj->guid);
                            $childrenpages_count++;
                        }

                    return $arr_currentpage_children;
            }