【问题标题】:How to display content from a child page within a hierarchial custom post type?如何在分层自定义帖子类型中显示子页面的内容?
【发布时间】:2013-05-13 18:20:43
【问题描述】:

我有一个分层的自定义帖子类型。它有6个页面,每个页面有3个子页面。

查看 6 个页面之一时,我需要显示其 3 个子/后代页面中的每个页面的内容(标题和摘录)。

这是我当前的循环:

<?php if(have_posts()):?>
    <?php query_posts('&post_type=how-we-do-it&post_parent=0');?>
    <?php while(have_posts()):the_post();?>
    <?php $color = get_post_meta( get_the_ID(), 'pointb_how-we-do-it-color', true ); ?>
      <div class="section">
          <div class="title">
            <h1 style="background:<?php echo $color;?> !important;">
              <?php the_title();?>
            </h1>
          </div>
          <div class="content">
            <div class="how-<?php the_slug();?>">the summary here. and here is child content:
                <div class="child">child content should be here.</div>
            </div>
          </div>
      </div>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>
    <?php endif;?>

我尝试了许多不同的方法来尝试完成我需要的东西,但它们都不能在自定义帖子类型中工作。以下是我尝试过的一些不同方法:

我尝试了此页面上的建议代码:http://wordpress.org/support/topic/display-child-pages-title-amp-content-on-parent-page

我也试过下面的代码:

$pageChildren = get_pages('child_of='.$post->ID');
if ( $pageChildren ) {
  foreach ( $pageChildren as $pageChild ) {
    echo '<h2><a href="' . get_permalink($pageChild->ID) . '">'. $pageChild->post_title.'</a></h2>
';
    if ($pageChild->post_excerpt){
      echo ''.$pageChild->post_excerpt.'

';
    }
  }
}

我尝试了许多其他方法,但我懒得保存,所以我无法展示它们。

我对此感到沮丧,并认为我应该把它扔在这里以获得一些新的观点。

【问题讨论】:

    标签: custom-post-type wordpress


    【解决方案1】:

    您的第一个示例的问题是您在重构查询之前调用了if(have_posts())

    第二个样本在 $post->ID 之后有一个悬空的'

    试试这个:

    $pageChildren = get_posts( 'post_type=how-we-do-it&post_parent='.$post->ID );
    

    【讨论】:

    • 我注意到悬空的 ' 但代码仍然不起作用。它返回来自顶级“页面”帖子类型的子帖子,但不返回自定义帖子类型中当前帖子 ID 的子页面。
    • 另外,我建议您将 post_name 更改为 how_we_do_it,因为您可能会遇到问题
    • 这可能意味着 $post->ID 没有设置。
    • 您必须在设置了 $post 的循环内。默认情况下未设置。
    【解决方案2】:

    根据上面 MarZab 的一些 cmets,我开始考虑帖子 ID。

    我对上面最初发布的代码块进行了以下调整,现在它可以完美运行:

    $pageChildren = get_pages('child_of='.$post->ID');
    

    现在:

    $post_id = get_the_ID();            
    $pageChildren = get_posts( 'post_type=how-we-do-it&echo=0&post_parent='.$post_id );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      相关资源
      最近更新 更多