首页 文章

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

提问于
浏览
1

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

<?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

    怎么样

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

    我假设$ featuredpost_cat是一个类别ID

  • 1

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

    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() 在页面上回显它们,上面的代码应该有效 . 如果不是这种情况,请告诉我们这些功能的用途 .


    编辑 . 删除了问号 .

相关问题