查询Wordpress自定义字段的正确方法是什么

时间:2012-04-21 12:14:37

标签: php wordpress

我有这个工作查询,可以在我的页面模板文件中成功获取自定义字段数据:

<?php $featuredpost_cat = get_field('featured_category_id'); ?>

如果我将其回显到页面中,我得到“23”自定义字段的值,所以我知道这是有效的,我想要做的就是获取该值并将其用作查询参数。

在我的页面上,我有这个:

<?php query_posts( $featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>

所有这一切都是忽略我的变量并返回网站上的最新帖子。

我希望这很清楚。

==编辑===

如果我不清楚,我想从页面获取一个自定义字段,这是一个类别ID,然后在页面模板的查询中使用它。

所以我将字段设置为类别ID:23然后在我的query_posts函数中调用它,以便我只返回该类别的帖子。

也许整页代码会有所帮助:template code

2 个答案:

答案 0 :(得分:1)

怎么样

<?php query_posts( 'cat='.$featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>

我认为$ featuredpost_cat是一个类别ID

答案 1 :(得分:0)

抱歉,我不明白您的第二个代码示例。您是否尝试使用三元运算符来实现此目的?

query_posts('cat='.$featuredpost_cat . '&posts_per_page=1');

if (have_posts()){
  while (have_posts()){
    the_post();
  }
}

query_posts()the_post()做了什么?如果query_post()抓取帖子,have_post()会检查帖子的存在,并the_post()在页面上回显它们,上面的代码应该有效。如果不是这种情况,请说明这些功能的作用。


编辑。删除了问号。

相关问题