wordpress 常见查询方法

方法1

$args = array ( 'post_parent' => 5 );
query_posts( $args );

if ( have_posts() ):
    while ( have_posts() ) :
        the_post();

        // Do stuff with the post content.
        the_title();
        the_permalink(); // Etc.

    endwhile;
else:
    // Insert any content or load a template for no posts found.
endif;

wp_reset_query();


方法2

query_posts( 'post_parent=5' );
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        ?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
    endwhile;
endif;
wp_reset_query();

方法3

// example args
$args = array( 'posts_per_page' => 3 );

// the query
$the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- start of the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php the_title(); ?>
        <?php the_excerpt(); ?>
    <?php endwhile; ?><!-- end of the loop -->

    <!-- put pagination functions here -->
    <?php wp_reset_postdata(); ?>

<?php else:  ?>

<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>

方法4

$lastposts = get_posts( array(
    'posts_per_page' => 3
) );

if ( $lastposts ) {
    foreach ( $lastposts as $post ) :
        setup_postdata( $post );
                the_content();
    endforeach; 
    wp_reset_postdata();
}