Wordpress - WP_Query,拉入多个 post
Wordpress - WP_Query, pulling in multiple post
对 Wordpress 和 PHP 有点陌生,正在尝试解决此问题。我有一个页面 (http://www.moderateindividual.com.php53-13.dfw1-1.websitetestlink.com/),在底部你可以看到一个包含 6 张图像的部分,我需要从具有自定义分类法的自定义 post 类型中提取这些图像。我现在所拥有的只是一遍又一遍地拉入一个 post,我将如何在该类别中拉入 6 个最新的 post?
这是我目前的代码
<?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-->
上面的代码是我在 google 中搜索可能有用的代码。
任何帮助或建议都将非常有用。
你只需要制作 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-->
对 Wordpress 和 PHP 有点陌生,正在尝试解决此问题。我有一个页面 (http://www.moderateindividual.com.php53-13.dfw1-1.websitetestlink.com/),在底部你可以看到一个包含 6 张图像的部分,我需要从具有自定义分类法的自定义 post 类型中提取这些图像。我现在所拥有的只是一遍又一遍地拉入一个 post,我将如何在该类别中拉入 6 个最新的 post?
这是我目前的代码
<?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-->
上面的代码是我在 google 中搜索可能有用的代码。
任何帮助或建议都将非常有用。
你只需要制作 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-->