我試圖涵蓋我的基本 PHP 知識,但我有點卡住了。我正在嘗試連接我的這段代碼:
if( have_rows('sales') ):
while( have_rows('sales') ) : the_row();
$dataPlatform = get_sub_field('platform');
endwhile;
endif;
應該從 ACF 轉發器欄位中獲取所有產品平臺(值的數量從 1 到大約 5 不等)。并將其添加到這個 foreach 回圈中:
global $post;
$terms = get_the_category( $post->ID, 'category' );
foreach($terms as $term) {
echo '<div data-type="'.$term->slug.'" data-platform="">';
}
顯然,我嘗試了最簡單的方法,如下所示:
global $post;
$terms = get_the_category( $post->ID, 'category' );
if( have_rows('sales') ):
while( have_rows('sales') ) : the_row();
$dataPlatform = get_sub_field('platform');
foreach($terms as $term) {
echo '<div data-type="'.$term->slug.'" data-platform="'. $dataPlatform .'">';
}
endwhile;
endif;
但它沒有按我的計劃作業。從理論上講,我希望像 data-platform="value1 value2.." 一樣填充名為 data-platform 的資料屬性,但我當前的代碼破壞了整個設計,因為它使用某些值復制了整個 div,并嵌套了它們。
我做錯了什么?
uj5u.com熱心網友回復:
這會讓你在不關閉它們的情況下創建一些 div。的價值是$dataPlatform
多少?此外,您應該使用esc_attr
or轉義您回顯的內容esc_html
。
foreach($terms as $term) {
echo '<div data-type="'.$term->slug.'" data-platform="'. $dataPlatform .'">';
}
更新:也許是這個?
global $post;
$terms = get_the_category( $post->ID, 'category' );
$dataPlatform = "";
if( have_rows('sales') ):
while( have_rows('sales') ) : the_row();
$dataPlatform .= get_sub_field('platform') . " ";
endwhile;
foreach($terms as $term) {
echo '<div data-type="'.$term->slug.'" data-platform="'. $dataPlatform .'">';
}
endif;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503634.html