web-dev-qa-db-fra.com

Formatage parent/enfant dans une liste d'articles d'un type d'article personnalisé

Je construis un long manuel pour les procédures de mes entreprises. Chaque section (et sous-section) est entrée en tant que publication parent et enfant dans un type de publication personnalisé. La boucle fonctionne correctement, mais j'aimerais pouvoir formater les enfants différemment des parents.

Je voudrais qu'il s'affiche comme suit:

<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-->

Des idées sur comment je pourrais être capable de réaliser ceci?

Comme demandé, voici le code du type de poste client:

<?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(); ?>
3
Jonathan Pyle

Je suppose que la partie que vous souhaitez modifier correspond à la zone de sortie du contenu (et non à la navigation). Si oui, vous pouvez faire ceci:

<?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 . '>';
?>
2
Michelle