【问题标题】:Render child page on parent page in Wordpress在 Wordpress 的父页面上呈现子页面
【发布时间】:2018-05-11 07:22:19
【问题描述】:

如何在 wordpress 的父页面上呈现子页面?我正在建立一个登陆页面网站,想法是使用子页面来制作登陆页面结构。

现在我在我的父页面模板中使用此代码:

$args = array(
    'post_type'         => 'page',
    'post_parent'       => $post->ID,                               
    'orderby'           => 'rand',
    'posts_per_page'    => 1,
    'no_found_rows'     => true
);
$child = new WP_Query($args);
var_dump($child->posts);

但它只是给了我一个数组,我需要完全呈现我的子页面的 HTML。

提前谢谢你)

【问题讨论】:

    标签: wordpress parent-child


    【解决方案1】:

    在这里试试这个代码:

    $args = array(
        'hierarchical' => 0,
        'child_of'     => $post->ID,
        'parent'       => $post->ID,
        'sort_column'  => 'menu_order, ID',
    );
    
    $pages = get_pages( $args );
    
    
    foreach ( $pages as $post ) : setup_postdata( $post );
    
       // child page html content here
    
    endforeach;
    
    //reset to the main page
    wp_reset_postdata();
    

    【讨论】:

      【解决方案2】:

      Matt Browne's answer的启发下,我终于找到了合适的解决方案

      functions.php

      function eatwings_show_page($pageid) 
      {
          global $post;
          $post = get_page($pageid);
          $tpl_slug = get_page_template_slug($post->ID);
          $tpl_slug_exp = explode('.', $tpl_slug);
          get_template_part($tpl_slug_exp[0]);
      }
      

      父页面模板.php:

      $args = array(
          'post_type'         => 'page',
          'post_parent'       => $post->ID,                               
          'orderby'           => 'menu_order, ID',
          'posts_per_page'    => 1,
          'no_found_rows'     => true
      );
      $child = new WP_Query($args);
      
      foreach($child->posts as $childpage)
      {
          eatwings_show_page($childpage->ID);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-17
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多