【问题标题】:Wordpress php: Way to test if pages are children of a parent page?Wordpress php:测试页面是否是父页面的子页面的方法?
【发布时间】:2009-05-21 03:07:06
【问题描述】:

在我的第一个 Wordpress 网站上工作,所以我确信这是一个非常基本的问题。我正在努力编写一个条件 php 语句,该语句在页面是父页面的子页面时执行特定操作。

例如,我想指定以“关于我们”页面为父页面的所有页面,而不是只指定一个页面,如下所示:

<?php if (is_page('About Us')) echo 'Hello World!'; ?>

我已经尝试过“child_of”函数,但它并不像我希望的那样简单。

当我使用下面的代码时,我得到一个语法错误 - 可能只是我不知道如何使用该函数:

<?php if (child_of('About Us')) echo 'Hello World!'; ?>

有什么建议吗?

【问题讨论】:

    标签: php wordpress parent if-statement


    【解决方案1】:

    将以下函数添加到您的 functions.php 主题文件中:

    function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
        global $post;         // load details about this page
        $anc = get_post_ancestors( $post->ID );
        foreach($anc as $ancestor) {
            if(is_page() && $ancestor == $pid) {
                return true;
            }
        }
        if(is_page()&&(is_page($pid))) 
                   return true;   // we're at the page or at a sub page
        else 
                   return false;  // we're elsewhere
    };
    

    然后你可以使用以下内容:

    if(is_tree('2')){ // 2 being the parent page id
       // Do something if the parent page of the current page has the id of two
    }
    

    参考:http://codex.wordpress.org/Conditional_Tags

    【讨论】:

      【解决方案2】:

      您收到错误是因为 WordPress 中没有 child_of() 函数之类的东西。

      child_of() 是一种使用 get_pages() 函数进行搜索的方式。

      $pages = get_pages('child_of=##');
      

      其中 ## 是“关于我们”页面的数字 ID(不是名称)。

      【讨论】:

      • 太棒了-我认为我使用不正确-感谢您清除它!我也只是让它以这种方式工作,以防其他人挣扎并想要一些选择: post_parent = 'About Us') echo 'Hello World!'; ?> 再次感谢!
      • Erg... 没关系。在所有情况下,它都假定列表中的第一个 if 语句。
      【解决方案3】:

      您想从子页面链接到父页面的简单解决方案; $post->post_parent 保存页面父级的 ID,如果有,则为 0,如果没有,则为 0:

      <?php
      if($post->post_parent !== 0){               
          print '<a href="'.get_permalink($post->post_parent).'">&larr; Back</a>';    
      }
      ?>
      

      所以对于这种情况,您需要更改 if() 以检查 $post->post_parent == $id_of_about_page。

      【讨论】:

        【解决方案4】:

        这是对我有用的最终代码 - 不确定这是否是做我想做的事情的正确方法,但会为了任何有我同样问题的人的利益而发布。

        我有一组右侧栏,每个栏都特定于网站的一个部分(每个父页面代表网站的一个部分)。

        我希望父页面和该父页面的所有子页面都拉出相同的特定右侧列。

        这就是我所做的:

        <?php 
        
        if (is_page('Page Name 1') || $post->post_parent == '##') {
        include (TEMPLATEPATH . '/right-1.php');
        
        } elseif (is_page('Page Name 2') || $post->post_parent == '##') {
        include (TEMPLATEPATH . '/right-2.php');
        
        } elseif (is_page('Page Name 3') || $post->post_parent == '##') {
        include (TEMPLATEPATH . '/right-3.php');
        
        } elseif (is_page('Page Name 4') || $post->post_parent == '##')
        include (TEMPLATEPATH . '/right-4.php');
        
        ?>
        

        ## 代表页面的 ID #。

        【讨论】:

          【解决方案5】:

          这段代码为我解决了这个问题,以防有人想知道替代方案:

          <?php
              global $post;
              if ($post->post_parent == 9) {
          
                  echo 'blah blah';
          
          }; ?>
          

          “9”是父页面的id。

          【讨论】:

            猜你喜欢
            • 2017-04-10
            • 2011-07-13
            • 1970-01-01
            • 2018-05-11
            • 2015-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多