垃圾站 WP教程 WordPress如何使用WP_Query来查询文章标题

WordPress如何使用WP_Query来查询文章标题

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

WordPress如何使用WP_Query来查询文章标题插图

<?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();?>

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

上一篇
下一篇
联系我们

联系我们

返回顶部