Wordpress - WP_Query,拉入多个帖子

时间:2015-04-01 16:21:50

标签: php wordpress custom-post-type wp-query custom-taxonomy

Wordpress和PHP有点新,并试图解决这个问题。我有一个页面(http://www.moderateindividual.com.php53-13.dfw1-1.websitetestlink.com/)和底部两个你可以看到一个包含6个图像的部分,我需要从具有自定义分类的自定义帖子类型中提取这些图像。我现在只是一遍又一遍地拉着一个帖子,我怎么能在这个类别的6个最新帖子中取得这个?

这是我到目前为止的代码

<?php 
//Define your custom post type name in the arguments
$args = array(
    'post_type' => 'news',   
    'tax_query' => array(
        array(
            'taxonomy' => 'news_category',
            'field'    => 'id',
            'terms'    => '47',
        ),
        ),
    );
//Define the loop based on arguments
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
      <div class="st_views">
        <div class="tab-1 st_view">
          <div class="st_view_inner">
            <div class="row cat-title">
                <p><span>As To Them Shall Seem Most Likely…</span> / Nullam quis dolor interdum erat dapibus aliquam. <a href="#">View all</a></p>
            </div>
            <div class="row top-stories">
              <div class="small-12 medium-8 large-8 columns main-news img-wrap"> <?php the_post_thumbnail('home-featured-thumb'); ?>
                <div class="story-title">
                  <h2><?php the_title(); ?></h2>
                  <p><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" />Read More</a></p>
                </div>
              </div>
              <!--End Large-8 Columns-->
              <div class="large-4 small-12 medium-4 columns">
                <div class="row news-top img-wrap"> <a href="<?php the_permalink(); ?>" /><?php the_post_thumbnail('home-thumb'); ?></a>
                  <div class="story-title-sub">
                    <a href="<?php the_permalink(); ?>" /><h2><?php the_title(); ?></h2></a>
                  </div>
                </div>
                <div class="row news-bottom img-wrap"> <a href="<?php the_permalink(); ?>" /><?php the_post_thumbnail('home-thumb'); ?></a>
                  <div class="story-title-sub">
                    <a href="<?php the_permalink(); ?>" /><h2><?php the_title(); ?></h2>
                  </div>
                </div>
              </div>
              <!--End Large-4 Columns--> 
            </div>
            <div class="row bottom-stories">
              <div class="large-4 medium-4 small-12 columns img-wrap"> <a href="<?php the_permalink(); ?>" /><?php the_post_thumbnail('home-thumb'); ?></a>
                <div class="story-title-sub">
                  <a href="<?php the_permalink(); ?>" /><h2><?php the_title(); ?></h2>
                </div>
              </div>
              <!--End Large-4 Columns-->
              <div class="large-4 medium-4 small-12 columns img-wrap"> <a href="<?php the_permalink(); ?>" /><?php the_post_thumbnail('home-thumb'); ?></a>
                <div class="story-title-sub">
                  <a href="<?php the_permalink(); ?>" /><h2><?php the_title(); ?></h2></a>
                </div>
              </div>
              <!--End Large-4 Columns-->
              <div class="large-4 medium-4 small-12 columns img-wrap"> <a href="<?php the_permalink(); ?>" /><?php the_post_thumbnail('home-thumb'); ?></a>
                <div class="story-title-sub">
                  <a href="<?php the_permalink(); ?>" /><h2><?php the_title(); ?></h2></a>
                </div>
              </div>
              <!--End Large-4 Columns--> 
            </div>
            <!--End Row--> 
          </div>
          <!--End st view inner--> 
        </div>
        <?php endwhile;?>
        <!--End tab 1 st view-->

上面的代码就是我只是在谷歌搜索可能有效的东西。

任何帮助或建议都会很棒。

1 个答案:

答案 0 :(得分:0)

你只需要制作2个循环并使用offset参数

<div class="st_views">
<div class="tab-1 st_view">
<div class="st_view_inner">
<?php 
//Define your custom post type name in the arguments
$args = array(
    'post_type' => 'news',   
    'tax_query' => array(
        array(
            'taxonomy' => 'news_category',
            'field'    => 'id',
            'terms'    => '47',
        ),
        ),
    'posts_per_page' => 3
    );
//Define the loop based on arguments
$loop = new WP_Query( $args ); if ( $loop->have_posts() ):?>
<div class="row cat-title">
<p><span>As To Them Shall Seem Most Likely…</span> / Nullam quis dolor interdum erat dapibus aliquam. <a href="#">View all</a></p>
</div>
<div class="row top-stories">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if( $loop->current_post == 0 ){?>     
<div class="small-12 medium-8 large-8 columns main-news img-wrap"> <?php the_post_thumbnail('home-featured-thumb'); ?>
     <div class="story-title">
           <h2><?php the_title(); ?></h2>
           <p><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>">Read More</a></p>
     </div>
</div>
<!--End Large-8 Columns-->
<?php } elseif( $loop->current_post == 1 ){?>
<div class="large-4 small-12 medium-4 columns">
<div class="row news-top img-wrap"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb'); ?></a>
       <div class="story-title-sub">
            <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
       </div>
</div>
<?php } elseif( $loop->current_post == 2 ){?>  
 <div class="row news-bottom img-wrap"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb'); ?></a>
      <div class="story-title-sub">
            <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
      </div>
</div>
</div>
<!--End Large-4 Columns--> 
<?php }?> 
<?php endwhile;?> 
</div> 
<?php  endif; //End Top Stories?>            
<?php 
//Define your custom post type name in the arguments
$args2 = array(
    'post_type' => 'news',   
    'tax_query' => array(
        array(
            'taxonomy' => 'news_category',
            'field'    => 'id',
            'terms'    => '47',
        ),
        ),
    'posts_per_page' => 3,
    'offset' => -3
    );
//Define the loop based on arguments
$loop2 = new WP_Query( $args2 ); if ( $loop2->have_posts() ):?>          
<div class="row bottom-stories">           
<?php while ( $loop2->have_posts() ) : $loop2->the_post(); ?>            
<div class="large-4 medium-4 small-12 columns img-wrap"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb'); ?></a>
     <div class="story-title-sub">
      <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
     </div>
</div>
<!--End Large-4 Columns-->
<?php endwhile; ?>
</div>
<!--End Bottom Row--> 
<?php endif;?>       
</div>
<!--End tab 1 st view inner-->         
</div>
<!--End tab 1 st view--> 
</div>
<!--End st view-->