Php 尝试仅从当前产品帖子获取属性(Woocommerce)
我在这里有一个奇怪的两难境地,我正试图弄清楚。基本上,我使用Woocommerce上的方法,使用下面的代码片段从每个产品帖子中获取产品属性Php 尝试仅从当前产品帖子获取属性(Woocommerce),php,arrays,wordpress,object,foreach,Php,Arrays,Wordpress,Object,Foreach,我在这里有一个奇怪的两难境地,我正试图弄清楚。基本上,我使用Woocommerce上的方法,使用下面的代码片段从每个产品帖子中获取产品属性 $productAttributesObject=$product->get_属性(“”) 然后将这些属性存储到数组中 foreach ($productAttributesObject as $i => $value) { $singleArray[] = $value->get_options(); } 不知何故,我创建的fore
$productAttributesObject=$product->get_属性(“”)
然后将这些属性存储到数组中
foreach ($productAttributesObject as $i => $value) {
$singleArray[] = $value->get_options();
}
不知何故,我创建的foreach似乎没有从产品帖子中清除值,基本上只是将它们堆叠在一起。为了解释清楚,我有两个产品帖子
一种产品包含一个名为水果
的属性,该属性包含以下术语:
100%橙汁|苹果|梨|菠萝|西番莲|红龙果|猴面包粉|葡萄提取物
其他产品包含一个名为Color
的属性,该属性包含以下术语:
红色|绿色
我的期望是,对于每个循环,它将属性附加到一个数组中
期望值
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(1) {
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(5) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
产品1属性
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(1) {
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(5) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
产品2属性
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(1) {
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(5) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
我得到的是数组似乎是堆叠在一起的,并从上一个产品中获取属性,而不是仅从当前产品中获取属性
现实
产品1属性
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(1) {
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(5) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
产品2属性
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(1) {
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
}
array(5) {
[0]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
[1]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
[2]=>
array(1) {
[0]=>
string(3) "Yes"
}
[3]=>
array(1) {
[0]=>
string(3) "Yes"
}
[4]=>
array(1) {
[0]=>
string(3) "Red"
}
}
如您所见,属性颜色与上一篇文章中的其他属性位于最后一个索引处
如何以编程方式仅从当前帖子获取属性?下面是完整的代码片段
<ul class="product-list">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php global $product ?>
<li class="product-post"
<?php
$productAttributesObject = $product->get_attributes('');
foreach ($productAttributesObject as $i => $value) {
$singleArray[] = $value->get_options();
}
foreach($singleArray as $val) {
foreach ($val as $v) {
$completeList[] = $v;
}
}
$arrayLowerCase = array_map('strtolower', $completeList);
$arrayConvert = str_replace(' ', '-', $arrayLowerCase);
$attributeTerms = implode(' ', $arrayConvert);
?>
data-type="<?php echo $attributeTerms ?>"
>
<div class="product-image-container">
<?php the_post_thumbnail('full', array('class' => 'product-image')) ?>
<div class="product-coaster"></div>
</div>
<h2 class="product-name"><?php the_title() ?></h2>
<?php the_excerpt() ?>
</li>
<?php endwhile; else: ?>
<p>no post</p>
<?php endif ?>
<?php wp_reset_query(); ?>
</ul>
数据类型=“”
>
无职位
您需要为每个产品重新初始化$singleArray
和$completeList
。要执行此操作,请添加
$singleArray = array();
$completeList = array();
在这一行之前:
foreach ($productAttributesObject as $i => $value) {
您需要为每个产品重新初始化
$singleArray
和$completeList
,添加$singleArray=array()$completeList=array()
在foreach($i=>productAttributesObject$value)
loophahahaha该死的,我不知道它这么简单。在这里,我正在绞尽脑汁试图弄明白这一点。我们都去过那里…@Nick你能不能把你的评论变成一个答案,这样我就可以把它标记为已解决:)