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

この$args行のは、クエリのパラメータを配置する場所です。

$wp_query = new WP_Query($args); 

したがって、そこに投稿制限を追加できます。

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

(コーデックスのその他のWPクエリパラメータ)

于 2012-06-22T19:50:48.640 に答える
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 に答える