我有一个如下的urlhttp://apploadny.appload.nu/tjanster/webbutveckling/我想获得“tjanster”页面的ID,我该怎么做?
Get ID of "root-page"
2 个回复
最合适的回答,由SO网友:alexleonard 整理而成
我认为您需要的是:
<?php
$post_id = $post->ID; //or somehow get the current post ID.
$ancestors = get_post_ancestors($post_id)
//$ancestors is an array of post IDs starting with the current post going up the root
//\'Pop\' the root ancestor out or returns the current ID if the post has no ancestors.
$root_id = (!empty($ancestors) ? array_pop($ancestors): $post_id);
有关更多信息,请检查:http://codex.wordpress.org/Function_Reference/get_post_ancestorsSO网友:mor7ifer
我认为比“亚历克斯·伦纳德”更好的解决方案是使用parent_id
post对象的参数。
$parent = ( $post_obj->parent_id != 0 ) ? $post_obj->parent_id : false;
还没有测试过,所以一定要检查拼写错误等等。结束