【问题标题】:Wordpress loop for one category set by an ACF由 ACF 设置的一个类别的 Wordpress 循环
【发布时间】:2018-09-28 17:22:11
【问题描述】:

所以我想返回一个只返回一个类别的 Wordpress 循环,所以目前我有这样的东西

<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

这很好用,完全符合我的要求

但我希望由 ACF 设置类别编号,所以我进行了设置,它在网站上将我的值作为字符串返回,一切都很好,所以现在我的代码看起来像这样

<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

我想做的是这样的

<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

我不认为我正在以正确的方式解决这个问题,并且似乎无法找到答案,因为我认为我不是在寻找正确的东西..如果有人可以指出我正确的方向,我真的很感激

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    在类似这样的查询帖子方法中使用 get_the_field 而不是 the_field

    query_posts('cat=get_the_field("category_number")');

    注意:- the_field() 是对字段包含的所有内容的回显。

    get_the_field 只是获取字段而不是回显它。

    希望对你有帮助:)

    【讨论】:

    • 嗯,好的,我明白了 - 我还不能让它工作,但我会尝试一些事情,看看我会怎么做
    【解决方案2】:

    你有没有尝试过类似的方法?

    https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command

    <?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <?php the_content(); ?>
    <?php endwhile; endif; ?>
    

    或者可能是这样的?

    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2021-02-24
      • 2018-08-07
      • 1970-01-01
      相关资源
      最近更新 更多