【问题标题】:use ACF taxonomy field term as value of wp_query使用 ACF 分类字段术语作为 wp_query 的值
【发布时间】:2015-02-13 19:33:48
【问题描述】:

我正在页面模板上运行 wp_query,以便从自定义分类“位置”过滤的自定义帖子类型“游览”中获取帖子。这种分类法是分层和有组织的“土地 -> 地区 -> 地点”。使用相关模板的当前页面具有由 ACF 分类字段分配的相同分类。这个 wp_query 实际上工作正常:

<?php $args = array( 'post_type' => 'tour', 'location' => 'meran' ); $featured_tour = new WP_Query( $args ); ?>
<?php if ( $featured_tour->have_posts() ) : while ( $featured_tour->have_posts() ) :     $featured_tour->the_post(); ?>
    <article>
        <h1><?php echo get_the_title(); ?></h1>
    </article>
<?php endwhile; else : ?>
    <p>No tour here! :( </p>
<?php endif; wp_reset_postdata(); ?>

我现在想用在 ACF“spot”字段中选择的术语替换位置名称,例如:'location' => 'function_that_inserts_current_ACF_term'。 有没有办法找回这个?谢谢你的建议。

【问题讨论】:

  • $term_field = get_field('my_taxonomy', $post-&gt;ID); 不成功?
  • 它给了我一个语法错误:解析错误:语法错误,意外的 T_STRING,在 /Applications/AMPPS/www/.../wp-content/themes/.../ 中期待 ')' template-leaflet-archive.php 在第 94 行。我正在尝试这样: 'tour', 'location' => '$term_field = get_field('spot', $后->ID);'); $featured_tour = new WP_Query($args); ?>
  • $term_field 变量和声明不应在您的 args 数组中。 location 数组键需要是一个字符串值,这是您从 ACF 获得的字段。
  • 如果我没听错的话,我不放心......你能告诉我使用什么代码吗?谢谢
  • &lt;?php $your_tax = get_field('tax', $page_id); //$page_id should be the page of 'Meran' ?&gt; &lt;?php $args = array( 'post_type' =&gt; 'tour', 'location' =&gt; $your_tax ); $featured_tour = new WP_Query( $args ); ?&gt; &lt;?php if ( $featured_tour-&gt;have_posts() ) : while ( $featured_tour-&gt;have_posts() ) : $featured_tour-&gt;the_post(); ?&gt; &lt;article&gt; &lt;h1&gt;&lt;?php echo get_the_title(); ?&gt;&lt;/h1&gt; &lt;/article&gt; &lt;?php endwhile; else : ?&gt; &lt;p&gt;No tour here! :( &lt;/p&gt; &lt;?php endif; wp_reset_postdata(); ?&gt; 如果当前页面中存在 ACF 税字段,您现在将获得它。

标签: advanced-custom-fields wordpress


【解决方案1】:
    <?php $your_tax = get_field('tax', $page_id); //$page_id should be the page of 'Meran'
$args = array( 'post_type' => 'tour', 'location' => $your_tax );

 $featured_tour = new WP_Query( $args ); ?> 

<?php if ( $featured_tour->have_posts() ) :
 while ( $featured_tour->have_posts() ) : $featured_tour->the_post(); ?> 
<article> 
   <h1><?php echo get_the_title(); ?></h1> 
</article> 
<?php endwhile; 
else : ?>
  <p>No tour here! :( </p> <?php endif; wp_reset_postdata(); ?> 

如果当前页面中存在 ACF 税字段,您现在将获得它。

【讨论】:

  • 我在另一个模板中尝试了代码,它给了我同样的错误:“警告:urlencode() 期望参数 1 是字符串,对象在 /..../wp-includes/formatting .php 在第 3835 行”。知道如何解决这个问题吗?谢谢。
  • 这当然是另一个问题,但是你能显示代码吗?
  • 这正是您在回答中给我的代码。我的问题中提供的代码可以正常工作,但是是“静态的”。您建议的版本按预期动态工作,但 wp 输出警告。
  • 很奇怪。如果您使用此代码只输入字符串而不是字段,会发生什么情况?
  • 然后我回到我开始的地方:这是一个工作 wp_query,静态,但没有抛出任何错误。
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
相关资源
最近更新 更多