开发

WordPress 自定义查询

自定义查询 ( Custom Queries ) 可根据不同的筛选查询条件,筛选出符合条件的文章列表。

<?php

// The Query
$args = array(
  'post_type' => 'post',
  'showposts' => 5
);

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
  echo '<ul>';
  while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
  }
  echo '</ul>';
} else {
  echo 'no posts found';
}

/* Restore original Post Data */
wp_reset_postdata();

参考链接

https://developer.wordpress.org/reference/classes/wp_query/

PUJI Design 朴及设计 (c) 2024. 沪ICP备17052229号