如何在不提供ID的情况下显示子页面,使其成为动态页面

时间:2018-10-11 作者:Gago

enter image description here

这是我的代码。这段代码显示了子页面标题。但我从父页面提供ID。有没有办法让它充满活力?我不想使用ID,因为它是静态的。。

  <?php
                  $childArgs = array(
                      \'sort_order\' => \'ASC\',
                      \'sort_column\' => \'menu_order\',
                      \'child_of\' => 127
                  );
                  $childList = get_pages($childArgs);
                  foreach ($childList as $child) { ?>

        <ul class="menu-items menu-level-1 menu-count-5">
          <li class="menu-item item-number-1  item-number-2 item-number-3 item-number-4 item-number-5 item-id-84283 item-odd item-page item-node item-alias-over-ons-de-winkel"><a href=""><?php echo $child->post_title; ?></a></li>
        <?php } ?>

2 个回复
最合适的回答,由SO网友:Gago 整理而成

//正确的代码

功能。php

  function get_page_parent_id( $id ) {
$args = array(
  \'sort_order\' => \'ASC\',
  \'sort_column\' => \'menu_order\',
  \'child_of\' => $id
);
$args = get_pages($args);

if(is_array($pages))
  $pageID = $id;
else {
  $pageID = wp_get_post_parent_id( $id );
}

return $pageID;


}
 ?>
第页。php

<?php
          $parentID = get_page_parent_id(get_the_ID());

            $childArgs = array(
            \'sort_order\' => \'ASC\',
            \'sort_column\' => \'menu_order\',
            \'child_of\' => $parentID
          ); ?>
    <div class="subnav">
      <h3 class="subnav-headline"><a href="/over-ons" class="c-dark"><?php echo get_the_title($parentID); ?></a></h3>

        <ul class="menu-items menu-level-1 menu-count-5">
             <?php $pages = get_pages($childArgs);
                foreach($pages as $page ) { ?>
          <li class="menu-item item-number-2 item-id-84286 item-even item-page item-node item-alias-over-ons-geschiedenis-leonidas"><a href="<?php echo get_the_permalink($page);?>"><?php echo $page->post_title;?></a></li>
            <?php   }   ?>
      </ul>
    </div>

SO网友:Pim

Try the following:

<?php

  global $post;

  $page_id = get_the_id();

  $childArgs = array(
    \'sort_order\' => \'ASC\',
    \'sort_column\' => \'menu_order\',
    \'child_of\' => $page_id
  );
  $childList = get_pages($childArgs);

  if($childList && (!$post->post_parent || is_page(127))){ ?>
    <ul class="menu-items menu-level-1 menu-count-5">
      <?php foreach ($childList as $child) { ?>
        <li class="menu-item item-number-1  item-number-2 item-number-3 item-number-4 item-number-5 item-id-84283 item-odd item-page item-node item-alias-over-ons-de-winkel">
          <a href=""><?php echo $child->post_title; ?></a>
        </li>
      <?php } ?>
    </ul>
  <?php } ?>
结束

相关推荐