我试图从灵活内容中的关系字段中获取ID,并将其推送到数组中
if (have_rows(\'studio_blocs\')) {
while(have_rows(\'studio_blocs\')) {
the_row();
$news = get_sub_field(\'studio_bio_list\');
if ($news) {
foreach($news as $item) {
$array[] = $item;
}
}
}
print_r($array);
}
就我得到这个输出的那一刻而言,我能得到一些帮助来获取ID并将其推送到我的数组中吗?
Array
(
[0] =>
[1] => WP_Post Object
(
[ID] => 88
[post_author] => 1
[post_date] => 2019-11-07 14:52:40
[post_date_gmt] => 2019-11-07 13:52:40
[post_content] =>
[post_title] => Sébastien Arkange
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => sebastien-arkange
[to_ping] =>
[pinged] =>
[post_modified] => 2019-11-07 14:53:27
[post_modified_gmt] => 2019-11-07 13:53:27
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://dev.mywebsite.com/?post_type=collaborateurs&p=88
[menu_order] => 8
[post_type] => collaborateurs
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
最合适的回答,由SO网友:WebElaine 整理而成
编辑自定义字段本身将是最简单的方法。在字段中的“Return Format”下,您可以将其从Post对象更改为Post ID。这样,当您调用数据时,您已经有了一个仅包含ID的数组。
如果由于某种原因无法更改字段,也可以将其编码为
<?php
if (have_rows(\'studio_blocs\')) {
while(have_rows(\'studio_blocs\')) {
the_row();
$news = get_sub_field(\'studio_bio_list\');
if ($news) {
foreach($news as $item) {
$array[] = $item->ID;
}
}
}
print_r($array);
}
?>
但是您会增加一些处理开销,因此更改字段是更好的解决方案。