WordPress 调用指定分类下的文章 支持分页

有时需要在博客或者企业网站中调用指定分类下的文章并进行分页,这里分享一下如何正确的调用WordPress 指定分类的文章。

添加分页导航

在主题根目录下的 functions.php 文件的 <?php 标签之后添加:

/**
 * 添加分页导航
 */

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

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


  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  $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'       => __('«上一页'),
    'next_text'       => __('下一页»'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

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

}

调用指定分类下的文章:

按需修改代码,4,5,6行

<?php
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $custom_args = array(
      'post_type'      => 'post', //文章类型
      'cat'            => 1, //分类 id
      'posts_per_page' => 10,//每页显示几篇文章
      '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 id="post-<?php the_ID(); ?>" <?php post_class('col-md-6'); ?> >
               
             
                <?php if (has_post_thumbnail( )) : ?>

                    <a href="<?php the_permalink();?>">
                        <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title();?>">
                    </a>

                <?php else: ?>
                    
                    <?php edit_post_link('添加缩略图'); ?>

                <?php endif; ?>
             

              <div class="text-box">
                  <h3 class="title">
                      <a href="<?php the_permalink();?>">
                        <?php the_title();?>
                      </a>
                  </h3>

                  <div class="text">
                    
                    <div class="meta">
                        <span>
                            标签:<?php the_tags( false, ', ', '' ); ?>
                        </span>

                        <span>
                            分类:<?php the_category( '/', '', false ); ?>
                        </span>

                        <span>
                            时间:<?php the_time( 'Y-m-d' ); ?>
                        </span>

                      
                    </div>
                  
                      <p>
                          <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 40,"..."); ?>
                      </p>
                  </div>
              </div>

              </article>
       
    <?php endwhile; ?>

    <?php
     site_pagination($custom_query->max_num_pages,"",$paged);
    ?>

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <article>抱歉,未发现文章</article>
<?php endif; ?>
</div>

如果你还定义了缩略图大小,可以将 19行的 代码:

<?php the_post_thumbnail_url(); ?>

改为:

<?php the_post_thumbnail_url('thumb-w6-h4' ); ?>

thumb-w6-h4 为你自定义添加到缩略图名称

 

静态首页调用

这里静态页面指的是自定义里的静态首页:

<div class="post-list">
<?php
  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
  $custom_args = array(
      'post_type'      => 'post', //文章类型
      'cat'            => 1, //分类 id
      'posts_per_page' => 12,//每页显示几篇文章
      'paged'          => $paged, //页码
      'page'           => $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 id="post-<?php the_ID(); ?>" <?php post_class('col-md-6'); ?> >
               
             
                <?php if (has_post_thumbnail( )) : ?>

                    <a href="<?php the_permalink();?>">
                        <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title();?>">
                    </a>

                <?php else: ?>
                    
                    <?php edit_post_link('添加缩略图'); ?>

                <?php endif; ?>
             

              <div class="text-box">
                  <h3 class="title">
                      <a href="<?php the_permalink();?>">
                        <?php the_title();?>
                      </a>
                  </h3>

                  <div class="text">
                    
                    <div class="meta">
                        <span>
                            标签:<?php the_tags( false, ', ', '' ); ?>
                        </span>

                        <span>
                            分类:<?php the_category( '/', '', false ); ?>
                        </span>

                        <span>
                            时间:<?php the_time( 'Y-m-d' ); ?>
                        </span>

                      
                    </div>
                  
                      <p>
                          <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 40,"..."); ?>
                      </p>
                  </div>
              </div>

              </article>
       
    <?php endwhile; ?>

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

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <article>抱歉,未发现文章</article>
<?php endif; ?>
</div>

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注