我需要这个PHP代码才能显示最新的帖子。也许在帖子上显示if语句。有任何想法吗?任何帮助表示赞赏。
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<a href="<?php the_permalink() ?>" <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail("events-thumb"); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo nl2br(get_post_meta($post->ID, 'proj_address', true)); ?></p>
<span></span>
<div style="clear:both;"></div>
</a>
<?php endwhile; ?>
<?php
endif;
$wp_query = $temp; //reset back to original query
?>
答案 0 :(得分:0)
此行中的$args
是您为查询添加任何参数的地方:
$wp_query = new WP_Query($args);
所以你可以在那里添加你的帖子限制:
$wp_query = new WP_Query( 'numberposts=1' );
(Codex中的更多WP查询参数)
答案 1 :(得分:0)
我更新了您的代码以获取最新信息。
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<a href="<?php the_permalink() ?>" <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail("events-thumb"); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo nl2br(get_post_meta($post->ID, 'proj_address', true)); ?></p>
<span></span>
<div style="clear:both;"></div>
</a>
<?php endwhile;
endif;
$wp_query = $temp; //reset back to original query
?>
答案 2 :(得分:0)
function home_post_limit( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 1 );
}
}
add_action( 'pre_get_posts', 'home_post_limit' );