WordPress教程

WordPress 查询自定义文章类型的文章和分类筛选

阿里云

WordPress 可以很方便的自定义文章类型,而且可以做到无限极分类,多分类多级别自定义筛选,提供了大量丰富的函数和开发文档供我们自由的定制,这篇文章将给大家介绍下如何查询自定义分类类型文章。

section

创建自定义文章类型,我们要用到 register_post_type 函数,可自定义很多参数,具体使用方法参见官方文档,这里做一个简单的示例。

也想出现在这里?联系我们
创客主机
  1. register_post_type( 'reform',
  2.  
  3.   array(
  4.  
  5.   'hierarchical' => true,
  6.  
  7.     'labels' => array(
  8.  
  9.       'name' => __( '维保改造' ),
  10.  
  11.       'singular_name' => __( '维保改造' )
  12.  
  13.     ),
  14.  
  15.     'public' => true,
  16.  
  17.     'has_archive' => false,
  18.  
  19.     'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
  20.  
  21.     'menu_icon' =>"dashicons-admin-tools"
  22.  
  23.   )
  24.  
  25. );

section

给自定义文章添加分类,文章类型添加完成一个,我们还可以给她添加一个或者多个分类,像有些文章的公共属性等。

  1. register_taxonomy(
  2.  
  3.   'elevator-brand',
  4.  
  5.   'reform',
  6.  
  7.   array(
  8.  
  9.     'label' => '电梯品牌',
  10.  
  11.     'rewrite' => array( 'slug' => 'elevator-brand' ),
  12.  
  13.     'hierarchical' => true
  14.  
  15.   )
  16.  
  17. );

注意上的第二个参数 reform,就是我们添加的自定义文章类型,如果你写成 post ,那么就是给现在的默认文档类型再添加一种分类类型,在使用上非常方便。

如果要给文章添加多个分类类型,那么只需要复制上面的代码,将函数的第一个参数、label 以及 rewrite 参数修改一下就可以了。

添加完成一个记得一定要在后台设置-固定链接保存一下,不然分类的链接将是 404。

section

查询自定义文章类型列表,接下来我们使用 new WP_Query() 查询自定义文章类型列表:

  1. $args = array(
  2.  
  3.   'post_type' => 'reform',
  4.  
  5.   //获取的文章类型
  6.  
  7.   'tax_query' => array(
  8.  
  9.     array(
  10.  
  11.       'taxonomy' => 'product_cat',
  12.  
  13.       //这里是自定义的分类类型
  14.  
  15.       'field' => 'id',
  16.  
  17.       //获取自定义分类的方式是通过ID获取
  18.  
  19.       //也可以通过别名slug的方式获取
  20.  
  21.       'terms' => 82,
  22.  
  23.       //自定义的分类的ID,
  24.  
  25.       //如果上一行你选择的是通过slug(别名方式获取)
  26.  
  27.       //则这里填写的是自定义分类的别名
  28.  
  29.     ),
  30.  
  31.   ),
  32.  
  33.   'numberposts'=>6 //获取的文章数量
  34.  
  35. );
  36.  
  37. $theQuery = new WP_Query( $args );

section

循环遍历输出文章

  1. <?php if ( $the_query->have_posts() ) : ?>
  2.  
  3. <?php while ( $the_query->have_posts() ) : 
  4.  
  5. $the_query->the_post(); ?>
  6.  
  7. <!--我自定义的文章格式-->
  8.  
  9. <!--值得一提的是这里循环会自动生成一个全局变量$post
  10.  
  11. 注意是$post而不是$posts
  12.  
  13. 通过这个变量你可以获取很多你想要的东西比如下面我获取当前文章的ID-->
  14.  
  15. <div class="people" id="<?php echo 'people'.$post->ID;?>">
  16.  
  17.   <ul id="people_info">
  18.  
  19.     <li><?php the_title()?></li>
  20.  
  21.   </ul>
  22.  
  23. </div> <!--文章循环结束-->
  24.  
  25. <?php endwhile; ?>
  26.  
  27. <?php else : ?>
  28.  
  29. <?php endif; ?>
  30.  
  31. <?php wp_reset_postdata();?>
  32.  
  33. <!--如果需要多次自定义查询最后把这句加上
  34.  
  35. 只查询一次也加上.养成一个好的习惯。-->

如果您不涉及查询自定义分类的话可以采用以下的方法:

  1. <?php
  2.  
  3. //courses为类型、8为篇数,rand为随机
  4.  
  5. $loop = new WP_Query( array(
  6.  
  7.   'post_type' => 'reform',
  8.  
  9.   'orderby'=>'rand',
  10.  
  11.   'posts_per_page' => 8
  12.  
  13. ) );
  14.  
  15. while ( $loop->have_posts() ) :
  16.  
  17. $loop->the_post(); ?>
  18.  
  19. //这里中间部分正常输出传统标题函数、链接函数等我就不写了
  20.  
  21. <?php endwhile;
  22.  
  23. wp_reset_query();?>

WordPress 查询自定义文章类型的文章和分类筛选

已有 317 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!