自定义帖子类型的帖子列表中的父/子格式

时间:2016-11-22 作者:Jonathan Pyle

我正在为我的公司程序编写一本很长的手册。我们将每个部分(和子部分)作为自定义帖子类型中的父帖子和子帖子输入。我有循环正常运行,但我希望能够设置不同于家长的孩子的格式。

我希望它显示如下:

<h1 class="parent">Parent Title 1</h1>
  <p>section content here...</p>
<h2 class="child"> Child Title 1</h2>
  <p>section content here...</p>
<h2 class="child"> Child Title 2</h2>
  <p>section content here...</p>
<h1 class="parent">Parent Title 2</h1>
  <p>section content here...</p>
<!--and so on-->
关于我如何实现这一目标,有什么想法吗?

根据要求,以下是客户职位类型的代码:

<?php get_header(); ?>
<!-- The loop for page content -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- Feature Image and Title Area -->
<div class="block block-inverse text-center">
  <div class="block-foreground">
    <h1 class="block-title text-ribbon text-ribbon-info"><?php the_title(); ?></h1>
    <h4 class="text-ribbon text-ribbon-info"><?php the_content(); ?></h4>
  </div>
  <div class="block-background">
    <div class="block block-fill-height app-header" style="background-image: url(<?php the_post_thumbnail_url(\'full\'); ?>)"></div>
  </div>
  <?php endwhile; else : ?>
    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
  <?php endif; ?>
</div>
<!-- Manual -->
<div class="block">
  <div class="container docs-content">
<!-- Manual ToC -->
    <ul id="markdown-toc">
      <li><h3><a href="#contents">Contents</a></h3></li>
      <?php       
        $args = array(
          \'post_parent\'=>\'0\',
          \'post_type\' => \'manual\', 
          \'posts_per_page\'=> \'-1\', 
          \'orderby\'=>\'menu_order\', 
          \'order\'=>\'asc\'
          );
        $parent_loop = new WP_Query( $args );
        while ( $parent_loop->have_posts() ) : $parent_loop->the_post();
      ?>      
        <li>
          <a href="#<?php echo the_slug(); ?>" id="markdown-toc-<?php echo the_slug(); ?>"><?php the_title(); ?></a>
        </li>
     <?php endwhile;?>
    </ul>
<!--Manual Content-->
    <?php       
      $args = array(
          \'post_type\' => \'manual\', 
          \'posts_per_page\'=>\'-1\', 
          \'orderby\'=>\'menu_order\', 
          \'order\'=>\'asc\'
        );
      $loop = new WP_Query( $args );
      while ( $loop->have_posts() ) : $loop->the_post();
    ?>
<!-- Article Title -->
    <h1 id="<?php echo the_slug(); ?>"><?php the_title(); ?></h1>
<!-- Comment Button -->
      <div class="btn-group">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          <?php comments_number(); ?> <span class="caret"></span>
        </button>     
          <div class="dropdown-menu">
            <div class="container">
              <?php comments_template(); ?>
            </div>
          </div>
      </div> <!-- Comment Button -->
<!-- Article Content -->
          <?php the_content(); ?>
      <?php endwhile; ?>
  </div> <!-- Manual Container -->
    <a href="#top" class="icon icon-chevron-with-circle-up docs-top"></a>
</div> <!-- Manual Block -->
<?php get_footer(); ?>

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

我猜您要更改的部分是实际的内容输出区域(而不是nav)。如果是这样,您可以这样做:

<?php       
$args = array(
    \'post_type\' => \'manual\', 
    \'posts_per_page\'=>\'-1\', 
    \'orderby\'=>\'menu_order\', 
    \'order\'=>\'asc\'
  );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

  $class=\'parent\';
  $header=\'h1\';
  if ($loop->post->post_parent !== 0) {
    $class=\'child\';
    $header=\'h2\';
  } 

  echo \'<\' . $header . \' class="\' . $class . \'" id="\' . $loop->post->ID . \'">\' . get_the_title() . \'</\' . $header . \'>\';
?>

相关推荐

rewrite rules hierarchical

我希望我的WordPress遵循以下规则:/productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.sin