WordPress 建网站时,可以通过 WP_Query()方法查询指定自定义字段来获取符合条件的值。例如查询某个字段值为 1 的所有文章,就可以如下写法。
- <?php
- $args = array(
- 'meta_query'=>array(
- array(
- 'key'=>'disabled',
- 'value'=>1,
- 'compare'=>'='
- )
- ),
- 'showposts' =>6,
- );
- $query = new WP_Query( $args );
- while ($query->have_posts()) : $query->the_post(); ?>
- <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
- <?php endwhile; ?>
- <?php wp_reset_query();?>
那么如何使用使用 WP_Query 来查询文章标题呢?可以使用以下的代码来实现。
- <?php
- $args = array(
- 's'=>'查询的关键词',
- 'orderby' => array(
- 'meta_value_num'=>'ASC'
- ),
- 'meta_key' => 'paixu',//按字段值排序
- );
- $query = new WP_Query( $args );
- while ($query->have_posts()) : $query->the_post(); ?>
- <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
- <?php endwhile; ?>
- <?php wp_reset_query();?>
这样就可以在文章标题里查询相应的关键词,得到自己需要的文章列表了。
- 1、本站大部分内容均收集于网络,若内容侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn,我们将第一时间处理!
- 2、本站资源所需价格并非资源售卖价格,是收集、整理、编辑以及本站运营的适当补贴,并且本站不提供任何免费技术服务。
- 3、所有资源仅限参考和学习,版权归原作者所有,更多请阅读无言说网络服务器协议。