我正在嘗試在博客文章中展示“特色產品”。這些特色產品將通過每個帖子后端的自定義字段帖子對象進行選擇。我已經寫下了我認為 PHP 應該是什么 - 我哪里出錯了?當我嘗試使用短代碼時,沒有出現任何代碼(但短代碼文本沒有顯示,因此肯定已添加)。謝謝 :) <?phpadd_shortcode('featuredproducts' , 'printfeaturedprod');function printfeaturedprod(){ $html = '';$instruments = get_field('featuredprod');if( $instruments ): $html .= '<div class="featuredproducts">'; $html .= '<h2 style="font-size:18px; font-family:poppins;">Featured in this video</h2>'; foreach( $instruments as $instruments ): $permalink = get_permalink( $instruments->ID ); $title = get_the_title( $instruments->ID ); $product = wc_get_product( $instruments->ID ); $price = $product->get_price(); $featured_img_url = get_the_post_thumbnail_url($instruments->ID, 'full'); $html .= '<div class="featuredproduct">'; $html .= '<img class="featuredproductimg" src="' . $featured_img_url . '">'; $html .= '<div class="proddetails">'; $html .= '<a class="producttitle" href="' . $permalink . '"><?php echo esc_html( $title ); ?></a>'; $html .= '<br><span class="productprice">£' . $price . '</span>'; $html .= '</div>'; $html .= '</div>'; endforeach; $html .= '</div>'; endif;}
1 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
您已在變量中構建了 HTML $html,但您沒有對其執行任何操作。短代碼不會自動知道您想要顯示 $html 變量,因此您需要在函數完成之前在末尾添加return( 或) 它:echo
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){? ? ? ??
? ? $html = '';
? ? /* your code here... */
? ? return $html;
}
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報
0/150
提交
取消