【发布时间】:2015-04-10 22:26:06
【问题描述】:
我正在尝试通过函数query_posts 在 Wordpress 页面模板中创建来自特定类别的帖子列表。
如果我直接在 query_posts 的数组值中输入 category_name 值(例如 'category_name' => 'france-category'),它就可以正常工作。
但我希望能够通过页面编辑器中的自定义字段来定义类别。
get_field("custom-field") 函数检索我在自定义字段中设置的值。
所以我尝试将自定义字段的值放入一个变量中,然后在数组的值中检索该变量:
<?php
$category = get_field("france-category");
// The Query
query_posts( array ( 'category_name' => 'echo $category' ) );
// The Loop
while ( have_posts() ) : the_post();
the_title();
endwhile;
// Reset Query
wp_reset_query();
?>
但由于某种原因,这不起作用。不能在数组中使用变量吗?或者谁能告诉我我做错了什么?
【问题讨论】:
-
仅将
'echo $category'替换为$category