在主页中添加带查询的区块

时间:2015-01-21 作者:Ds.sorosh

我想我的2个帖子块显示的类别不是最近的,也不是类别。

enter image description here

<!-- Latest posts -->
<div class="home-section">
  <div class="container"> <div class="row">

    <div class="col-md-12">
      <div class="section-title">
        <h2> <?php echo _e( \'گالری\', \'\' );?></h2>
        <span> <?php echo _e( \'نمونه کارهای انجام شده\', \'\' );?></span>
      </div>
    </div>

    <div class="grid-cover">
      <?php 
      $count=3;
      $args = array(
            \'posts_per_page\' => $count,  // Limit count 
        );
      $recent = new WP_Query( array( \'category__in\' => "3" ) );

      $query = new WP_Query( $args );
      while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="col-sm-4">
        <div class="grid-box">
          <div class="grid-pic">
            <?php 
              $thumb = get_post_thumbnail_id();
              $img_url = wp_get_attachment_url( $thumb,\'full\' ); //get full URL to image (use "large" or "medium" if the images too big)
              $image = aq_resize( $img_url, 720, 480, true,true,true ); //resize & crop the image
              ?>
            <?php if($image) : ?>
                <a href="<?php the_permalink(); ?>"> <img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /> </a>
            <?php endif; ?>
          </div>
          <div class="grid-entry">
            <div class="grid_title">
              <h2> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </h2>
              <div class="grid-meta">
                <span> <?php echo _e( \'این پست در:\', \'fabthemes\' );?> <?php the_category(\', \'); ?></span>
              </div>
            </div>
            <div class="entry-content">
              <p>
                <?php
                  $excerpt = get_the_excerpt();
                  echo string_limit_words($excerpt,25);
                ?>
              </p>
            </div>
          </div>
        </div>  
        </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    </div>

  </div> </div>
</div>
<!-- Latest Posts end -->


<!-- Latest posts -->
<div class="home-section">
  <div class="container"> <div class="row">

    <div class="col-md-12">
      <div class="section-title">
        <h2> <?php echo _e( \'مقالات\', \'\' );?> </h2>
        <span> <?php echo _e( \'نمونه کارهای انجام شده\', \'\' );?></span>
      </div>
    </div>

    <div class="grid-cover">
      <?php 
      $count=3; 
      $args = array(

            \'posts_per_page\' => $count,  // Limit count
        );
      $query = new WP_Query( \'cat=1\' );
      $query = new WP_Query( $args );
      while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="col-sm-4">
        <div class="grid-box">
          <div class="grid-pic">
            <?php 
              $thumb = get_post_thumbnail_id();
              $img_url = wp_get_attachment_url( $thumb,\'full\' ); //get full URL to image (use "large" or "medium" if the images too big)
              $image = aq_resize( $img_url, 720, 480, true,true,true ); //resize & crop the image
              ?>
            <?php if($image) : ?>
                <a href="<?php the_permalink(); ?>"> <img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /> </a>
            <?php endif; ?>
          </div>
          <div class="grid-entry">
            <div class="grid_title">
              <h2> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </h2>
              <div class="grid-meta">
                <span> <?php echo _e( \'این پست در:\', \'fabthemes\' );?> <?php the_category(\', \'); ?></span>
              </div>
            </div>
            <div class="entry-content">
              <p>
                <?php
                  $excerpt = get_the_excerpt();
                  echo string_limit_words($excerpt,25);
                ?>
              </p>
            </div>
          </div>
        </div>  
        </div>

        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    </div>

  </div> </div>
</div>
<!-- Latest Posts end -->

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

在两个查询中,您都做错了。您正在运行的WP_Query 对于每个参数集

$args = array( \'posts_per_page\' => $count, // Limit count ); 
$query = new WP_Query( \'cat=1\' ); 
$query = new WP_Query( $args );
应该是

$args = array( \'posts_per_page\' => $count, \'cat\' => 1 );
$query = new WP_Query( $args );

结束