WordPress教程

WordPress使用WP_Query来查询文章标题

阿里云

WordPress 建网站时,可以通过 WP_Query()方法查询指定自定义字段来获取符合条件的值。例如查询某个字段值为 1 的所有文章,就可以如下写法。

  1. <?php
  2. $args = array(
  3. 'meta_query'=>array(
  4. array(
  5. 'key'=>'disabled',
  6. 'value'=>1,
  7. 'compare'=>'='
  8. )
  9. ),
  10. 'showposts' =>6,
  11. );
  12. $query = new WP_Query( $args );
  13. while ($query->have_posts()) : $query->the_post(); ?>
  14. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  15. <?php endwhile; ?>
  16. <?php wp_reset_query();?>
也想出现在这里?联系我们
创客主机

那么如何使用使用 WP_Query 来查询文章标题呢?可以使用以下的代码来实现。

  1. <?php
  2. $args = array(
  3. 's'=>'查询的关键词',
  4. 'orderby' => array(
  5. 'meta_value_num'=>'ASC'
  6. ),
  7. 'meta_key' => 'paixu',//按字段值排序
  8. );
  9. $query = new WP_Query( $args );
  10. while ($query->have_posts()) : $query->the_post(); ?>
  11. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  12. <?php endwhile; ?>
  13. <?php wp_reset_query();?>

这样就可以在文章标题里查询相应的关键词,得到自己需要的文章列表了。

WordPress 使用 WP_Query 来查询文章标题

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

收藏
(0)

发表回复

热销模板

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

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