簡體   English   中英

如何在Wordpress中獲取當前選定的菜單項

[英]How to get current, selected menu item in Wordpress

請告訴我如何獲取//讀取菜單中當前所選項目的父項...。​​Wordpress // PHP

例:

菜單結構:

A -aa -ab -ac

B -ba -bb -bc

因此,如果用戶選擇,說“ ba”項目,那么我需要獲得“ B”項目(...)

謝謝

您在說的是面包屑。 這應該會有所幫助: https : //wordpress.org/support/topic/display-page-parent-on-page-with-title

本質上: $post->post_parent將為您提供父頁面的ID。

如果嘗試獲取當前頁面的父頁面,則可以使用“ 獲取帖子父ID” ,也可以使用祖先https://codex.wordpress.org/Function_Reference/get_post_ancestors

謝謝大家的提示和指導。

我已經通過以下方式解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM