亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用帖子上的短代碼調用帖子對象(產品)自定義字段

如何使用帖子上的短代碼調用帖子對象(產品)自定義字段

PHP
翻翻過去那場雪 2023-09-22 14:56:53
我正在嘗試在博客文章中展示“特色產品”。這些特色產品將通過每個帖子后端的自定義字段帖子對象進行選擇。我已經寫下了我認為 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;

}

查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號