调用最新发表的文章

  <?php
                    $recentPosts = new WP_Query();
                    $recentPosts->query('showposts=30'); //显示数量
                ?>
                <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

                    <article <?php post_class('col-md-5 col-sm-4 col-xs-6');?>>
                      <div class="card">
                        <a href="<?php the_permalink(); ?>">
                            <img class="lazy-load" src="<?php image_fill();?>" data-src="<?php echo get_template_directory_uri(); ?>/inc/functions/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&w=250&h=200&zc=1" alt="<?php the_title();?>">
                        </a>
                        <div class="text">
                          <h6><a href="<?php the_permalink();?>"><?php the_title();?></a></h6>
                        </div>
                      </div>
                    </article>
                    <?php endwhile; ?>
                    <?php wp_reset_query(); ?>

调用指定分类下的文章(支持翻页)

functions.php 添加:

/**
 * 分页
 */

function doocii_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   *
   * It's good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /**
   * We construct the pagination arguments to enter into our paginate_links
   * function.
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text' => __('<i class="fa fa-angle-left"></i> 上一页'),
    'next_text' => __('下一页  <i class="fa fa-angle-right"></i>'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination col-md-12'>";
      echo "<span class='page-numbers page-num'>当前第" . $paged . '页' . " 共" . $numpages . '页'."</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

}

不是静态页面调用:

   <?php
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
      $custom_args = array(
          'post_type' => 'post', //文章类型
          'cat'=>218,  //分类
          'posts_per_page' => 40, //每页显示
          'paged' => $paged //页码
        );
      $custom_query = new WP_Query( $custom_args ); ?>
      <?php if ( $custom_query->have_posts() ) : ?>

        <!-- 开始循环 -->
        <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
          <article <?php post_class('col-md-5 col-sm-4 col-xs-6');?>>
            <div class="card">
              <a href="<?php the_permalink(); ?>">
                  <img class="lazy-load" src="<?php image_fill();?>" data-src="<?php echo get_template_directory_uri(); ?>/inc/functions/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&w=250&h=200&zc=1" alt="<?php the_title();?>">
              </a>
              <div class="text">
                <h6><a href="<?php the_permalink();?>"><?php the_title();?></a></h6>
              </div>
            </div>
          </article>
        <?php endwhile; ?>
        <!-- 循环结束 -->


        <?php
           if (function_exists(doocii_pagination)) {
             doocii_pagination($custom_query->max_num_pages,"",$paged);
           }
         ?>

      <?php wp_reset_postdata(); ?>

      <?php else:  ?>
        <p>当前分类未发现文章</p>
      <?php endif; ?>

调用指定分类下的文章2

<?php  
    $the_query = new WP_Query(
        array(
            'cat'=>3, //分类id
            'posts_per_page' => 4, // 最多显示的文章数
        ) );
    if ( $the_query->have_posts() ) {
        // 获取分类目录名称
            $category = get_the_category();
            echo '<div class="cat-title"><span>'.$category[0]->cat_name.'<a href="'.get_category_link($category[0]->cat_ID).'"><i class="iconfont">更多</i></a></span></div>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();


?>
    <article id="post-<?php the_ID();?>" <?php post_class();?>>
        <h2><a href="<?php the_permalink(); ?>" class="topic"> <?php the_title();?> </a></h2>
        <div class="content">
        <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"..."); ?>

        </div>
        <time><?php the_time('Y-m-d');?></time>
    </article >
<?php  
}
}
else {  
    echo '<li><a href="#">该分类没有文章</a></li>';
}
    wp_reset_postdata();
?>

调用指定自定义字段文章

 <?php


                      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
                      $popularpost = array(
                          'post_type' => 'post', //文章类型
                          'meta_key' => 'wpb_post_views_count',//自定义字段名
                          'orderby' => 'meta_value_num',
                          'order' => 'DESC',
                          'posts_per_page' => 40, //每页显示
                          'paged' => $paged //页码
                        );
                      $custom_query = new WP_Query( $popularpost ); ?>
                      <?php if ( $custom_query->have_posts() ) : ?>

                        <!-- 开始循环 -->
                        <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
                          <article <?php post_class('col-md-5 col-sm-4 col-xs-6');?>>
                            <div class="card">
                              <a href="<?php the_permalink(); ?>">
                                  <img class="lazy-load" src="<?php image_fill();?>" data-src="<?php echo get_template_directory_uri(); ?>/inc/functions/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&w=250&h=200&zc=1" alt="<?php the_title();?>">
                              </a>
                              <div class="text">
                                <h6><a href="<?php the_permalink();?>"><?php the_title();?></a></h6>
                              </div>
                            </div>
                          </article>
                        <?php endwhile; ?>
                        <!-- 循环结束 -->

                        <?php
                           if (function_exists(doocii_pagination)) {
                             doocii_pagination($custom_query->max_num_pages,"",$paged);
                           }
                         ?>

                      <?php wp_reset_postdata(); ?>
                      <?php else:  ?>
                        <p>当前分类未发现文章</p>
                      <?php endif; ?>