2016-10-28 8 views
1

私のpage-home.phpには、上記のセクションに最新の投稿がすべて表示されます。投稿の特定のカテゴリのみを表示する

elseif($key == 'latest_post_block'){ ?> 
    <?php if(of_get_option('latest_post_block_visibility') != '0'): ?> 
    <section id="blog" class="clearfix"> 
     <div class="ak-container"> 
     <?php if(of_get_option('latest_post_title') || of_get_option('latest_post_desc')): ?> 
      <div class="section-title-wrap"> 
       <?php if(of_get_option('latest_post_title')): ?> 
        <h1 class="main-title"><?php echo esc_attr(of_get_option('latest_post_title'));?></h1> 
       <?php endif; ?> 

       <?php if(of_get_option('latest_post_desc')): ?> 
        <div class="sub-desc"> 
         <?php echo esc_textarea(of_get_option('latest_post_desc'));?> 
        </div> 
       <?php endif; ?> 
      </div> 
     <?php endif; ?> 
     </div> 

     <?php 
     if(of_get_option('latest_post_count') > 0) : 
     $args = array(
      'posts_per_page' => of_get_option('latest_post_count') 
      ); 
     $query = new WP_Query($args);     
     if($query->have_posts()): 
     ?> 
     <div class="blog-block-wrapper clearfix"> 
      <div class="ak-container"> 
      <div class="block-block-wrap"> 
       <?php 
       $count = 0; 
       while($query->have_posts()): 
        $query->the_post(); 
        $count++; 
        $class = ($count % 2 == 0) ? "right-blog" : "left-blog"; 
       ?> 
        <div class="blog-block <?php echo $class; ?>"> 
         <div class="blog-image"> 
          <?php if(has_post_thumbnail()): 
           $big_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full'); 
           $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-thumbnail'); 
          ?> 
          <a class="blog-img-wrap" href="<?php the_permalink(); ?>"> 
          <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>"> 
          </a> 
          <div class="blog-overlay"> 
           <div class="blog-anchor-wrap"> 
            <a class="search fancybox-gallery" data-lightbox-gallery="gallery" href="<?php echo $big_image[0]; ?>"> <i class="fa fa-search"></i></a> 
            <a class="link" href="<?php the_permalink(); ?>"> <i class="fa fa-link"> </i> </a> 
           </div> 
          </div> 
          <?php 
          endif; ?> 
         </div> 

         <div class="blog-content-wrapper clearfix"> 
          <div class="blog-date-wrap"> 
           <div class="blog-date"> 
            <?php echo get_the_date('d'); ?><br/> 
            <?php echo get_the_date('M'); ?> 
           </div> 
          </div> 
          <div class="blog-content"> 
           <h3 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></h3> 
           <div class="blog-desc"><?php echo accesspress_letter_count(get_the_content(),'200'); ?></div> 
          </div> 
          <div class="clearfix"> </div> 
          <div class="blog-comments-wrap clearfix"> 
           <div class="blog-comments"> 
            <span><a class="author" href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"> <i class="fa fa-user"> </i><?php echo get_the_author(); ?></a></span> 
            <span><a class="comment" href="<?php echo get_comments_link($post->ID); ?>"> <i class="fa fa-comments"> </i><?php echo get_comments_number(); ?></a></span> 
            <?php if(has_category()): ?> 
            <span> 
            <i class="fa fa-folder"></i><?php echo get_the_category_list(', '); ?> 
            </span> 
            <?php endif; ?> 
            <?php if(has_tag()): ?> 
            <span> 
            <i class="fa fa-tags"></i><?php echo get_the_tag_list('' , ', '); ?> 
            </span> 
            <?php endif; ?> 
           </div> 
          </div> 
         </div> 
        </div> 
        <?php 
         if($count % 2 == 0){ 
          echo '<div class="clearfix"></div>'; 
         } 
        ?> 
        <?php 
        endwhile; ?> 
       </div> 
      </div> 
     </div> <!-- blog-block-wrapper end --> 
     <?php 
     endif; 
     wp_reset_postdata(); 
     endif; ?> 
    </section> <!-- blog --> 
    <?php endif; ?> 

しかし、私は投稿の特定のカテゴリのみを表示し、それらのすべてではないことを望みます。これはできますか?

答えて

1

はい、あなたはそのような$args配列変更することができます:それは働いた

$args = array(
    'posts_per_page' => of_get_option('latest_post_count'), 
    'category_name' => 'cat' // Category slug 
); 
+0

$args = array( 'posts_per_page' => of_get_option('latest_post_count'), 'cat' => 4 // Category ID ); 

または、カテゴリスラッグで照会するの! IDの4,5でカテゴリを表示する必要がある場合は、 '' cat '=> 4 ||と書くでしょう。 'cat' => 5'? – yaylitzis

+1

@ Nat95いいえ、あなたはIDの配列を渡すことができるので、 '' cat '=> array(4,5) ' – George

関連する問題