如何在Wordpress中创建子页面到当前页面,创世纪主题

时间:2018-03-10 17:51:52

标签: php wordpress genesis

我想列出所有关闭的孩子到当前页面,而不是大孩子或兄弟姐妹,但是当我尝试这个时,我会得到所有页面。

function list_my_child_pages( $post ) {

    $list_p = array(
        'sort_column' => 'menu_order, post_title',
        'child_of' => $post->ID,
        'sort_order' => 'ASC'
    );

    $children = get_pages($list_p); 

    $result = "<ul>";
    foreach ( $children as $child )
    {
        $child_id = $child->ID;
        $url  = get_permalink( $child_id );
        $thumb = get_the_post_thumbnail($child_id, array(240, 240));
        $title= $child->post_title;

        $link = "<a href='$url'><div class='child_page_thumb'>$thumb</div><div class='child_page_title'>$title</div></a>";

        $result .= "<li>$link</li>";
    }

    $result .= "</ul>";

    echo $result;
}

对我来说,看起来它不应该列出这个查询的兄弟姐妹,但确实如此。我如何删除孙子孙女?

1 个答案:

答案 0 :(得分:0)

我通常直接在模板中执行此操作,这是我的代码:

class Car {
  constructor(props) {
    this.props = props;
  }

  brake() {
    this.activateBrakes();
    this.doneBraking(Car);
  }

  activateBrakes() {
    console.log('1');
  }
  
  doneBraking(ctor) {
    if (ctor === this.constructor) {
      this.props.onBrake();
    }
  }
}

class Prius extends Car {
  brake() {
    super.brake();

    this.activateRegenerativeBrakingSystem();
    this.doneBraking(Prius);
  }

  activateRegenerativeBrakingSystem() {
    console.log('2');
  }
}

var onBrake = () => console.log('after');

var car = new Car({onBrake});
car.brake();

var prius = new Prius({onBrake});
prius.brake();

如果你不想获得一个列表,你可以在一个数组中存储例如以下的ID:

<?php
// Showing the list of child pages
// Check if current page has children pages
$children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);

// if current page got children pages :
if (sizeof($children) > 0):                 
    $args = array(
        'post_status'       => ' publish',
        'post_type'         => 'page',
        'posts_per_page'    => -1,
        'post_parent'       => $post->ID,
        'order'             => 'ASC',
        'orderby'           => 'date',
    );

    $parent = new WP_Query( $args );

    // Showing a list in the template
    if ( $parent->have_posts() ) : ?>
        <aside class="item-list">
            <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
                <a class="item-from-list" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php if (has_post_thumbnail()) {the_post_thumbnail('thumbnail', array('class' => 'alignleft'));} ?>
                    <h2><?php the_title(); ?></h2>
                    <?php atm_content(20, true); ?>
                </a>
            <?php endwhile; ?>
        </aside>
    <?php endif; wp_reset_query(); ?>
<?php endif;// end if sizeof($children) ?>