experience

经验

zw-1988.com

home > 经验 > wordpress 获取当前页面的ID值

2017.11.27 wordpress 获取当前页面的ID值

父级页面和子集页面用一个模板
创建模板的方法是

<?php
/*
template name:aboutus
*/
get_header(); ?>

获取当前页面的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://www.1988-zw.comrdpress/?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” 
 }
«
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com