1

すべてのカテゴリ (子カテゴリを含む) の最新の 3 つの投稿を表示するコードがあり、これらの投稿は既に親カテゴリに表示されているため、これらの子カテゴリから投稿を除外したいと考えています。

たとえば、親カテゴリとしてプリンター カテゴリがあり、子カテゴリとして (プリンター アクセサリ、.....、.....) があります。
したがって、それぞれの最新の 3 つの投稿が表示され、子カテゴリ (プリンター アクセサリ、.....、...) とその投稿が表示されないように除外したいと考えています。

コードは次のとおりです。

$cat_args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'child_of' => 0
);

$categories =   get_categories($cat_args); 

foreach($categories as $category) {
    echo '<dl>';
    echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';

     $post_args = array(
      'numberposts' => 3,
      'category' => $category->term_id
    );

    $posts = get_posts($post_args);

    foreach($posts as $post) {
    ?>
        <dd>
                    <div class="allincat">

                    <div class="catmeta">                    
                    <span class="authinfo"> 
                    <div class="authimg"></div>
                     <?php the_author(); ?> | <?php the_time('jS F Y') ?> </span>


                    </div>

                    <div class="allincatimg">
                    <?php the_post_thumbnail(array(50,50)); ?> </div>
                    <div class="allincattit">                    
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </div>





                    </div>

                    </dd>
    <?php
    }
    echo '<div class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category->name ) . '" ' . '>View all items in ' . $category->name.'</a></div>';
    echo '</dl>';
}
4

1 に答える 1

0

引数配列を構築した後、投稿をクエリします。

<?php
    $args = array(
        'cat'      => 22, // your parent cat id here 
        'order'    => 'ASC'
        'orderby' => 'name',
        'posts_per_page' => 3
    );

    query_posts( $args ); ?>

          <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
              <dd>
                <div class="allincat">
                  <!-- content here -->
                </div>
              </dd>
            <?php endwhile; ?>
          <?php endif; ?>
于 2012-05-31T15:26:03.303 に答える