wordpress获取当前页面的ID值

在wodrpress的主题制作或者主题修改的时候,经常需要获取当前页面的ID值。所以说获取当前页面的ID值,还是相当重要的。因为是小白,所以说没有老鸟那么熟练,所以在这些天定制wordpress主题的过程中的积累了些获取当前加载页面的ID值://(1)方法一: $postid = get_the_ID(); echo ...

在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() : 当前页面对象下所有的属性

{ ["ID"]=> int(339) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2017-08-04 15:13:09" ["post_date_gmt"]=> string(19) "2017-08-04 07:13:09" ["post_content"]=> string(3040) "三一重工股份有限公司由三一集团投资创建于1994年”。" ["post_title"]=> string(12) "三一介绍" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(36) "三一介绍" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2017-08-04 16:42:44" ["post_modified_gmt"]=> string(19) "2017-08-04 08:42:44" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) //获取父级的ID -> 很重要,经常用["guid"]=> string(39) "http://localhost/wordpress/?page_id=339" ["menu_order"]=> int(0) ["post_type"]=> string(4) "page" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw"  } 

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

WORDPRESS 5.0版本后,恢复经典默认编辑器的方法

2023-2-16 15:54:36

wordpress教程

WordPress获取文章摘要函数:get_the_excerpt

2023-2-16 15:54:38

【腾讯云】11.11云上盛惠!云服务器首年1.8折起,买1年送3个月!
11.11云上盛惠!海量产品·轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。
查看更多相关信息>>
在线咨询
站长

(工作日 10:00 - 22:30 为您服务)

2026-07-07 23:49:57

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

猜你想问:

  • 购买的模板免费包安装吗?

  • 这个演示地址有吗?

  • 购买vip会员可以下载哪些模板?

您的留言我们已经收到,我们将会尽快跟您联系!
取消
立即选择任一渠道联系我们