Login

Record it, wordpress, get the page ID

Author:neo yang Time:2023/11/06 Read: 6870

I have always used

global $post; $id=$post->ID;

I just found out today that there is a problem.

If you add a shortcode to the page, and the shortcode outputs a list (such as a list of articles in a certain category), then this method cannot get the page ID.

That is to say, if there is a loop in the page, then the above method cannot obtain the page ID.

List several methods to obtain the page ID:

1.global

Affected by circulation.

global $post; $id=$post->ID;

2,get_the_ID()

Affected by circulation.

$postid = get_the_ID(); echo $postid;

 

3.get_queried_object_id()

Not affected by circulation. recommend.

$current_id = get_queried_object_id(); echo $current_id;

4.get_queried_object()

Not affected by circulation.

$object = get_queried_object(); $id = $object -> ID; echo $id;
tags:


copyright © www.lyustu.com all rights reserved.
Theme: TheMoon V3.0. Author:neo yang