wordpress獲取當前頁面的ID值

2021-08-16 22:11:21 字數 980 閱讀 9677

在wodrpress的主題製作或者主題修改的時候,經常需要獲取當前頁面的id值。

所以說獲取當前頁面的id值,還是相當重要的。因為是小白,所以說沒有老鳥那麼熟練,所以在這些天定製wordpress主題的過程中的積累了些獲取當前載入頁面的id值:

//(1)方法一:

$postid = get_the_id();

echo $postid; //列印出當前載入頁面的id值

//(2)方法二:

$current_id = get_queried_object_id();

echo $current_id; //列印出當前載入頁面的id值

//(3)方法三:

$c_id_object = get_queried_object();

$c_id = $c_id_object -> id;

echo $c_id; //列印出當前載入頁面的id值

新增:

//(4)方法四

global $post;

$id = $post -> id;

echo $id; //列印出當前頁面的id

//如何獲取父級頁面的id:

global $post;

$id = $post -> id;

$parent = get_post_ancestors($post -> id);

print_r($parent); //列印出 array ( [0] => 339 )

//獲取父級id第二種方案:

global $post;

$parent_id = $post -> post_parent;

echo $parent_id; //列印出父級頁面的id

注釋:get_queried_object() : 當前頁面物件下所有的屬性

WordPress 獲取當前頁面 ID 的幾大方法

在很多的 wordpress 主題或者外掛程式功能的開發中,我們總是需要獲取到 wordpress 給每個頁面定義的 id,不然也某些情況下是無法確定這是哪乙個頁面,針對於文章或者頁面的 id 獲取基本可以使用 get the id 這個函式來直接獲取,但是在迴圈外該函式是無法獲取到值的。方法一 1...

c 獲取當前頁面URl

2 通過js獲取 thisdloc document.location thisurl document.url thishref document.location.href thissloc self.location.href thistloc top.location.href thispl...

獲取當前頁面的URL

window location host 返回url 的主機部分,例如 www.com window location hostname 返回www.com window location href 返回整個url字串 window location pathname 返回 a index.php或...