0

我需要这个 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
    ?>
4

3 回答 3

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
        ?>
于 2012-06-22T20:05:04.733 回答
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' );
于 2013-11-29T19:48:02.560 回答
0

$args这一行中,您可以为查询放置任何参数:

$wp_query = new WP_Query($args); 

所以你可以在那里添加你的帖子限制:

$wp_query = new WP_Query( 'numberposts=1' );

( Codex中有更多 WP 查询参数)

于 2012-06-22T19:50:48.640 回答