Php 在wordpress中用post对象替换数组中的值
我有一系列不同的帖子和产品:Php 在wordpress中用post对象替换数组中的值,php,arrays,wordpress,advanced-custom-fields,Php,Arrays,Wordpress,Advanced Custom Fields,我有一系列不同的帖子和产品: array (size=2) 0 => array (size=2) 'acf_fc_layout' => string 'post' 'linked_post' => int 6802 1 => array (size=2) 'acf_fc_layout' => string 'product' 'linked_produc
array (size=2)
0 =>
array (size=2)
'acf_fc_layout' => string 'post'
'linked_post' => int 6802
1 =>
array (size=2)
'acf_fc_layout' => string 'product'
'linked_product' => int 5140
我现在的问题是,我希望post/and-product数组包含整个post/product对象,而不仅仅是ID。我还使用了twig,这使得在视图中很难查询对象。因此,我尝试从后端执行此操作:
// Getting the array of Posts and Products
$gallerix = get_field('gallerix_layout', 'options');
// Trying to overwrite the value in the loop
foreach ($gallerix as $gallerix_item) {
if ( $gallerix_item->acf_fc_layout == 'product' ) {
$gallerix_item->linked_product = wc_get_product( $gallerix_item->linked_product );
} elseif ( $gallerix_item->acf_fc_layout == 'post' ) {
$gallerix_item->linked_post = get_post( $gallerix_item->linked_post );
}
}
// Pass the array to Timber/Twig
$context['gallerix'] = $gallerix;
// Render twig template
Timber::render( 'views/template.twig', $context );
希望有人能理解我的问题。非常感谢您的支持 我认为您的问题在于您正在更新内部的临时变量
foreach()
循环。并且您的更改不会存储在$gallerix
阵列中
试试这个:
<?php
foreach ($gallerix as $key => $gallerix_item) {
//...
$gallerix[$key]->linked_product = wc_get_product(...);
//...
}
?>
而不是更改$gallerix_项
变量