您的位置:首页 > 其它

wordpress获取当前页面的ID值

2018-03-14 09:35 176 查看
在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);  //打印
4000
出 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"
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: