Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查WordPress帖子是否有孩子和兄弟姐妹?_Wordpress_Parent_Siblings - Fatal编程技术网

如何检查WordPress帖子是否有孩子和兄弟姐妹?

如何检查WordPress帖子是否有孩子和兄弟姐妹?,wordpress,parent,siblings,Wordpress,Parent,Siblings,我需要隐藏一段代码,以免在某些页面中出现。这些页面都是ID为8194的父页面的子页面和同级页面 要在子页面中隐藏代码,我使用的是if(get_post_字段('post_parent')!=8194),但问题是有几个同级页面,并且代码对它们不起作用,它只在子页面中起作用 这是我的页面层次结构: Parent page 1 - Child page 1 -- Sibling page 1 -- Sibling page 2 ... -- Sibling page 10 如何在同级页面中隐藏代码

我需要隐藏一段代码,以免在某些页面中出现。这些页面都是ID为8194的父页面的子页面和同级页面

要在子页面中隐藏代码,我使用的是
if(get_post_字段('post_parent')!=8194)
,但问题是有几个同级页面,并且代码对它们不起作用,它只在子页面中起作用

这是我的页面层次结构:

Parent page 1
- Child page 1
-- Sibling page 1
-- Sibling page 2
...
-- Sibling page 10
如何在同级页面中隐藏代码


谢谢

您可以使用
wp\u get\u post\u parent\u id()
函数通过与
get\u the\u id()
函数进行比较来检查当前页面是父页面还是子页面<代码>是页面()这里,只是一个冗余

<?php
/**
* wp_get_post_parent_id
* Returns the ID of the post’s parent.
* @link https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/
*
* get_the_ID
* Retrieve the ID of the current item in the WordPress Loop.
* @link https://developer.wordpress.org/reference/functions/get_the_ID/
*/
if( is_page() && wp_get_post_parent_id( get_the_ID() ) ):
  //child
  echo 'This is THE Child, Grogu';
else:
  //parent
  echo 'This is THE Mandalorian, Mando';
endif; ?>

如果我理解正确,您希望找到最顶端的家长

为此,我将使用
get\u post\u祖先()

试着这样做:

global $post;
$parents = get_post_ancestors($post->ID);
// Get the 'top most' parent page ID, or return 0 if there is no parent:
$top_parent_id = ($parents) ? $parents[count($parents)-1]: 0;

if ($top_parent_id != 8194) {
...
}

**从评论中更新OP第二个问题的代码:**

global $post;

// Don't show the code by default:
$show_code = false;

// This is an array of post ids where you want to show the code regardless of the parents id
$always_show_the_code_on_these_posts = array(111, 222, 333);

// Check if current post id is on the list:
if ( in_array($post->ID, $always_show_the_code_on_these_posts) ) {
    $show_code = true;
} else {
    // ...and only if it's not on the list run the previous test:
    $parents = get_post_ancestors($post->ID);

    // Get the 'top most' parent page ID, or return 0 if there is no parent:
    $top_parent_id = ($parents) ? $parents[count($parents)-1]: 0;

    if ($top_parent_id != 8194) {
        $show_code = true;
    }
}

if ($show_code) {   
...
}

我得到了同样的结果。我可以在子页面上隐藏代码,但不能在兄弟页面上隐藏,因为我不知道如何检查。也许可以添加孩子的ID?但我也有很多这样的孩子,所以我正在尝试将其自动化。是的,我需要找到最优秀的家长。您的代码正在运行,但它也将我的代码隐藏在父代码中。我只需要将其隐藏在孩子和兄弟姐妹的帖子中。哦,这是因为建议的代码在没有家长的情况下返回当前帖子ID,请尝试使用此修改版本(我编辑了我的答案)。太棒了,它工作得非常好。非常感谢。我遇到了一个小问题。有没有一种方法可以使用给定的ID从代码中排除单个子页面?我尝试在条件中添加排除,但它不起作用,我猜代码的另一部分正在绕过它。当你说“排除”时,你的意思是想在该页面id上显示或隐藏代码?非常感谢!它工作得很好。