加入收藏 | 设为首页 | 会员中心 | 我要投稿 航空爱好网 (https://www.ikongjun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

wordpress获取当前页面的ID值方式

发布时间:2022-06-22 10:31:00 所属栏目:教程 来源:互联网
导读:在wodrpress的主题制作或者主题修改的时候,经常需要获取当前页面的ID值。 所以说获取当前页面的ID值,还是相当重要的。因为是小白,所以说没有老鸟那么熟练,所以在这些天定制wordpress主题的过程中的积累了些获取当前加载页面的ID值: (1)方法一: $post
  在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"  
   }  。

(编辑:航空爱好网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!