我在Wordpress的另一个数组中创建了一个数组,我需要从每个数组中获取ID值。
这是我用来提取数组的代码
<?php $pages = get_field(\'products\');
get_field(\'product_id\', $pages->ID); ?>
如果我使用
print_r($pages)
我得到以下输出:
Array
(
[0] => Array
(
[product_id] => WP_Post Object
(
[ID] => 35238
[post_author] => 13
[post_date] => 2014-06-05 15:33:27
[post_date_gmt] => 2014-06-05 15:33:27
[post_content] => 40G parallel optics transceivers (40GBASE-SR4) ...
[post_title] => 40G QSFP+ (MTP/MPO) to 4x10G SFP+ (8xLC) Assembly
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => 40g-qsfp-mpomtp-4x10g-sfp-8xlc-assembly
[to_ping] =>
[pinged] =>
[post_modified] => 2014-07-01 08:34:38
[post_modified_gmt] => 2014-07-01 08:34:38
[post_content_filtered] =>
[post_parent] => 36259
[guid] => http://www.fibrefab.com/?page_id=35238
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[1] => Array
(
[product_id] => WP_Post Object
(
[ID] => 34484
[post_author] => 13
[post_date] => 2014-06-05 15:16:04
[post_date_gmt] => 2014-06-05 15:16:04
[post_content] => MTP/MPO QSFP+ trunk/ patchcord assemblies are ...
[post_title] => 40G QSFP+ (MTP/MPO) to QSFP+ Assembly
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mtpmpo-microcable-qsfp-40gb-trunk-patchcord-assembly
[to_ping] =>
[pinged] =>
[post_modified] => 2014-07-01 08:34:24
[post_modified_gmt] => 2014-07-01 08:34:24
[post_content_filtered] =>
[post_parent] => 36259
[guid] => http://www.fibrefab.com/?page_id=34484
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
如何显示每个阵列的[ID]?
最合适的回答,由SO网友:Eric Holmes 整理而成
看起来您正在使用高级自定义字段。您的“产品ID”存储的是对象,而不仅仅是ID。下面显示了如何访问post ID,否则只需更改产品ID的字段保存选项即可。
这很简单foreach
陈述
foreach( $pages as $page ) {
echo $page[\'product_id\']->ID;
}