/* 336x280*/
在 WordPress 中,可以通过分类或者标签的形式聚合文章,如果你想要通过自定义字段来聚合,也未尝不可。这篇文章会告诉你如何按自定义字段获取 WordPress 文章列表。你有可能会在 WordPress 自定义页面模板中用到它。
<?php
// The Query to show a specific Custom Field
$the_query = new WP_Query('meta_key=color');
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
现在如果你想要按某个特定值的自定义字段获取文章列表,只要改变以下代码中的查询条件参数即可:
$the_query = new WP_Query( 'meta_value=blue' );
现在,如果你想分别按照自定义字段名称和取值来获取文章列表,比如获取所有自定义字段名称为“color”,并且值为“blue”的文章,你可以这么来搞:
$the_query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );
通过自定义字段可以将 WordPress 扩展出很多实用的功能,详细可以参考官方文档关于 WP_Query Parameters 的介绍。
/* 336x280*/
版权所有,转载请注明出处。
转载自 <a href="http://www.mangguo.org/display-wordpress-post-only-if-specific-custom-field/" title="按自定义字段获取 WordPress 文章列表" rel="bookmark">按自定义字段获取 WordPress 文章列表 | 芒果小站</a>
已经有 1 条群众意见
- 秋天
#1/2011-12-13 21:35这种是比较好,但是,怎样控制文章显示条数呢? 回应
我简单说几句