简体   繁体   English

WordPress-WP_Query,拉入多个帖子

[英]Wordpress - WP_Query, pulling in multiple post

Somewhat new to Wordpress and PHP and trying to work though this issue. Wordpress和PHP的一些新功能,尽管有此问题,但仍可以正常工作。 I have a page ( http://www.moderateindividual.com.php53-13.dfw1-1.websitetestlink.com/ ) and twards the bottom you can see a section with 6 images, I need those to be pulled in from a custom post type with a custom taxonomy. 我有一个页面( http://www.moderateindividual.com.php53-13.dfw1-1.websitetestlink.com/ ),然后在底部看到一个包含6张图像的部分,我需要将这些图像从具有自定义分类法的自定义帖子类型。 What I have now its just pulling in one post over and over, how would I make this pull in the 6 latest post in that category? 我现在所获得的只是一遍又一遍的帖子,我将如何在该类别中最新发布的6篇文章中做出这样的选择?

Here is my code I have so far 这是我到目前为止的代码

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

The code above was me just searching the google for something that might work. 上面的代码是我只是在google上搜索可能有用的东西。

Any help or advice would be great. 任何帮助或建议都很好。

you just have to make 2 loops and use the offset parameter 您只需要进行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-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM