在 WordPress 中检查 post_parent = 0
Checking whether post_parent = 0 in WordPress
我正在使用 WordPress 使用主题创建网站,每个页面的顶部都有这个 PHP,但我对它的作用感到困惑。
<?php
global $post;
global $wp_query;
if ( $post->post_parent != 0 ) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
};
?>
我只是想知道是否有人可以准确解释这是做什么的?我认为它会检查 post_parent
id
是否为 0
,这在 WordPress 中是不允许的,并将 post id
设置为 post_parent
但我不是 100% 确定。
据我所知,此代码段执行以下操作:
- 加载全局 post 对象。
- 加载全局查询对象。
- 检查(或尝试检查)当前 post 是否有父过滤器 (?)。
- 如果有,它将变量 $thePostID 设置为当前 post 对象的 post_parent 参数的 ID。
- 如果没有,它将wp_query中的post id设置为当前post的id。
总而言之,这似乎很不必要,表现不佳而且很奇怪Oo。
ist真的有效吗? ^^
如果可能,我会建议您使用其他主题 ;-)
I think it checks to see if the post_parent id is 0 which isn’t allowed in WordPress
允许$post->post_parent
为0
。如果值为 0
,则仅表示该页面是顶级页面。
具有 $post->post_parent
而非 0
的页面是另一个页面的子页面。
例如,以这个页面结构为例:
id page_title post_parent
1 Home 0
2 About 0
3 Staff 2
4 History 2
5 Contact 0
生成的 page/menu 结构将是:
- 首页
- 关于
- 员工
- 历史
- 联系方式
有问题的代码:
if ($post->post_parent != 0) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
}
我不确定为什么您的主题可能有代码,但一个可能的原因可能是获取与当前页面相关的菜单。如果您正在查看顶级页面(即 $post->post_parent == 0
),那么它会显示所有子页面,或者如果您正在查看子页面,菜单可能会显示所有同级页面。
使用此方法生成的示例菜单
将其添加到您的 functions.php
文件中,以便在整个主题中都可以访问它。
/**
* Get top parent for the current page
*
* If the page is the highest level page, it will return its own ID, or
* if the page has parent(s) it will get the highest level page ID.
*
* @return integer
*/
function get_top_parent_page_id() {
global $post;
$ancestors = $post->ancestors;
// Check if the page is a child page (any level)
if ($ancestors) {
// Get the ID of top-level page from the tree
return end($ancestors);
} else {
// The page is the top level, so use its own ID
return $post->ID;
}
}
将此代码添加到您要显示菜单的主题中。您需要对其进行自定义以满足您的特定需求,但它为您提供了一个示例,说明为什么有人可能会使用您询问的代码。
// Get the highest level page ID
$top_page_id = get_top_parent_page_id();
// Display basic menu for child or sibling pages
$args = array(
'depth' => 1,
'title_li' => FALSE,
'sort_column' => 'menu_order, post_title',
'child_of' => $top_page_id
);
echo wp_list_pages($args);
我正在使用 WordPress 使用主题创建网站,每个页面的顶部都有这个 PHP,但我对它的作用感到困惑。
<?php
global $post;
global $wp_query;
if ( $post->post_parent != 0 ) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
};
?>
我只是想知道是否有人可以准确解释这是做什么的?我认为它会检查 post_parent
id
是否为 0
,这在 WordPress 中是不允许的,并将 post id
设置为 post_parent
但我不是 100% 确定。
据我所知,此代码段执行以下操作:
- 加载全局 post 对象。
- 加载全局查询对象。
- 检查(或尝试检查)当前 post 是否有父过滤器 (?)。
- 如果有,它将变量 $thePostID 设置为当前 post 对象的 post_parent 参数的 ID。
- 如果没有,它将wp_query中的post id设置为当前post的id。
总而言之,这似乎很不必要,表现不佳而且很奇怪Oo。 ist真的有效吗? ^^
如果可能,我会建议您使用其他主题 ;-)
I think it checks to see if the post_parent id is 0 which isn’t allowed in WordPress
允许$post->post_parent
为0
。如果值为 0
,则仅表示该页面是顶级页面。
具有 $post->post_parent
而非 0
的页面是另一个页面的子页面。
例如,以这个页面结构为例:
id page_title post_parent
1 Home 0
2 About 0
3 Staff 2
4 History 2
5 Contact 0
生成的 page/menu 结构将是:
- 首页
- 关于
- 员工
- 历史
- 联系方式
有问题的代码:
if ($post->post_parent != 0) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
}
我不确定为什么您的主题可能有代码,但一个可能的原因可能是获取与当前页面相关的菜单。如果您正在查看顶级页面(即 $post->post_parent == 0
),那么它会显示所有子页面,或者如果您正在查看子页面,菜单可能会显示所有同级页面。
使用此方法生成的示例菜单
将其添加到您的 functions.php
文件中,以便在整个主题中都可以访问它。
/**
* Get top parent for the current page
*
* If the page is the highest level page, it will return its own ID, or
* if the page has parent(s) it will get the highest level page ID.
*
* @return integer
*/
function get_top_parent_page_id() {
global $post;
$ancestors = $post->ancestors;
// Check if the page is a child page (any level)
if ($ancestors) {
// Get the ID of top-level page from the tree
return end($ancestors);
} else {
// The page is the top level, so use its own ID
return $post->ID;
}
}
将此代码添加到您要显示菜单的主题中。您需要对其进行自定义以满足您的特定需求,但它为您提供了一个示例,说明为什么有人可能会使用您询问的代码。
// Get the highest level page ID
$top_page_id = get_top_parent_page_id();
// Display basic menu for child or sibling pages
$args = array(
'depth' => 1,
'title_li' => FALSE,
'sort_column' => 'menu_order, post_title',
'child_of' => $top_page_id
);
echo wp_list_pages($args);