Wordpress get_posts()类别参数问题

时间:2016-10-19 15:52:52

标签: php wordpress

我正在使用get_posts()函数显示帖子网格,并尝试从自定义字段控制类别参数值。

如果category参数从echo函数获取值,我的代码可以工作,例如:

$args = array( 
'numberposts' => 6,
'category'         => '<?php echo "25";?>'
,);

但是,当我做

之类的事情时,它并没有
$catid = get_field( "id_of_product_category" );
$args = array( 
'numberposts' => 6,
'category'         => '<?php echo $catid;?>'
,);

同时我能够

echo $catid;

在页面上,它确实显示在自定义字段中设置的正确值。

为什么我无法让它改变类别参数呢?

1 个答案:

答案 0 :(得分:0)

我刚刚意识到你的get_field()缺少第二个参数。如果您没有指定具有该名称字段的帖子的特定ID,则很可能默认为当前帖子的值(如果存在)。因此,您基本上需要循环遍历get_posts()以获取具有您正在寻找的值的当前帖子,然后执行您正在寻找的功能。暴露更多你的能力会有所帮助。

// Get your custom field from ACF based on field name and the ID of the post
$catid = get_field( "id_of_product_category", $post->ID );

$args = array( 
'numberposts' => 6,
'category'    => $catid
);