由ACF设置的一个类别的Wordpress循环

时间:2018-04-18 12:49:22

标签: wordpress advanced-custom-fields

所以我想返回一个只返回一个类别的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; ?>

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

2 个答案:

答案 0 :(得分:0)

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

query_posts( '猫= get_the_field( “category_number”)');

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

get_the_field只是让字段不回显它。

希望它会对你有所帮助:)。

答案 1 :(得分:0)

您是否尝试过这些方法?

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/

相关问题