[英]Display latest post in category
雖然wordpress堆棧可以回答這個問題,但我認為我的問題更多是與基本PHP邏輯有關,而不是wordpress問題。
我的問題是我的代碼顯示的是類別中的每個帖子,而不僅僅是最新的帖子。 我的代碼必須在此類別的顯示帖子中說諸如foreach帖子之類的內容,但是我想說的是, 僅顯示最新的帖子 。
$post_type = 'post';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$args = array('taxonomy' => $taxonomy, 'term' => $term->slug, 'posts_per_page' => 1, 'orderby' => 'modified','category' => $str );
$posts = new WP_Query( $args );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
if(has_term('double portrait','twin')) {
get_post( &$post, $output = OBJECT, $filter = 'raw' )
}
endwhile; endif;
endforeach;
endforeach;
wp_reset_postdata();
這是我目前的代碼。 幫助表示贊賞。
一種簡單的方法,無需太多更改,您可以在第一次迭代后暫停一段時間,即:
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); if(has_term('double portrait','twin')) { get_post( &$post, $output = OBJECT, $filter = 'raw' ); break; } endwhile; endif;
您可能也考慮使用此函數: <?php wp_get_recent_posts( $args, $output ) ?>
這里有更多信息: http : //codex.wordpress.org/Function_Reference/wp_get_recent_posts
$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, $output = ARRAY_A );
這將為您提供10條最近的帖子。 您可以使用$ args數組來獲得適合您的不同結果。
$args = array(
'posts_per_page' => 1,
'orderby' => 'modified',
'category_name' => $str,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'twin',
'field' => 'slug',
'terms' => array('double portrait','landscape')
)
)
);
$myPosts = get_posts($args);
foreach ( $myPosts as $post ) :
setup_postdata( $post );
echo get_template_part( 'twin-feature' ); ?>
<?php endforeach; wp_reset_postdata(); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.