【问题标题】:Selecting child pages with a specific parent page doesn't work选择具有特定父页面的子页面不起作用
【发布时间】:2019-02-14 18:32:36
【问题描述】:

当特定父页面的子页面可视化时,我需要添加特定功能。

我的父页面的 ID = 115,它有 7 个子页面。我使用了以下代码

global $post; // load details about this page
 if(is_page()&&($post->post_parent== '115)) {
           echo ' This is a subpage';
 }
else {
           echo 'This is not a subpage';
}

虽然它应该可以工作,但当我可视化其中一个子页面时,输出回显是“这不是子页面” -> 这意味着它不会将其识别为子页面。

怎么了?非常感谢

【问题讨论】:

  • 我刚刚编辑了问题以使其更清晰。

标签: wordpress


【解决方案1】:

想要检测 WordPress 上的特定页面是否有子页面?

这个函数派上用场了,只需将它添加到你的functions.php中:

function has_subpage() {
            global $post;
        if($post->post_parent){
            $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
        } else {
            $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        } if ($children) {
            echo ' This is a subpage';
        } else {
            echo ' This is a not subpage';
        }
    }

【讨论】:

  • 不,实际上我只需要检测页面是否是特定父页面的子页面,因为在这些子页面上,我必须将特征图像添加为滑块。我不想在每个单一页面上手动执行此操作,因此我认为通过 functions.php 执行此操作更容易,如本示例中所述 [link] itsmereal.com/quick-snippets-for-enfold-child-theme/…
  • 您需要是父页面还是检查当前页面是否有子页面?
  • 我需要知道一个页面是否是特定父页面的子页面-> 我的父页面 id 是 115,我必须将滑块添加到其所有子页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多