我使用以下方法將自定義欄位添加到 WooCommerce 單一產品頁面functions.php:
add_action( 'woocommerce_after_single_product_summary', 'auction_information_field', 4 );
但是,我在使用 Divi Builder 時遇到塊的位置放置問題。
因為當 divi builder 被激活時,它被放置在 Divi builder 區域之前的外面。但是當我使用默認的標準編輯器時作業正常。
所以我有興趣通過將add_action欄位的函式轉換為短代碼來解決這個問題。所以短代碼可以留在 中functions.php,我可以將短代碼放在 divi builder 模塊中,使其位于正確的位置。
雖然我不確定如何轉換成短代碼。
任何建議將不勝感激。
<div class="et_pb_row property_page_row">
<div class="property-content">
<h2>Auction Details</h2>
<div class="property-overview">
<ul>
<li>
Auction Status
<strong><?php
$type = $product->get_auction_status();
switch ( $type ) {
case 'non-started':
echo esc_attr__( 'Not Started', 'yith-auctions-for-woocommerce' );
break;
case 'started':
echo esc_attr__( 'Started', 'yith-auctions-for-woocommerce' );
break;
case 'finished':
echo esc_attr__( 'Finished', 'yith-auctions-for-woocommerce' ) ;
break;
}
?>
</strong>
</li>
<li>
Auction Type <strong><?php echo $product->get_auction_type(); ?></strong>
</li>
<li>
Auction Start Date
<strong><?php
$dateinic = $product->get_start_date();
if ( $dateinic ) {
$format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
$format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
$format = $format_date . ' ' . $format_time;
$date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateinic ), $format );
echo $date;
}
?>
</strong>
</li>
<li>
Auction End Date
<strong><?php
$dateclose = $product->get_end_date();
if ( $dateclose ) {
$format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
$format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
$format = $format_date . ' ' . $format_time;
$date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateclose ), $format );
echo $date;
}
?>
</strong>
</li>
</ul>
</div>
</div>
</div>
<?php
}
uj5u.com熱心網友回復:
如代碼中的 WordPress Shortcode API 所述:https : //codex.wordpress.org/Shortcode_API
如果短代碼生成大量 HTML,則 ob_start 可用于捕獲輸出并將其轉換為字串,如下所示:-
function my_shortcode() {
ob_start();
// Here you can put your additional HTML or PHP code.
?> <HTML> <here> ... <?php
return ob_get_clean();
}
add_shortcode('my-shortcode', 'my_shortcode');
如果您不使用輸出緩沖,則代碼通常會顯示在頁面上的內容之前。
uj5u.com熱心網友回復:
您可以使用add_shortcode. 你可以使用 like [auction_information_field]。試試下面的代碼。
function auction_information_field_callback() {
if( is_singular( 'product' ) ){
global $product;
ob_start();
if ( 'auction' === $product->get_type() ) { ?>
<div class="et_pb_row property_page_row">
<div class="property-content">
<h2>Auction Details</h2>
<div class="property-overview">
<ul>
<li>
Auction Status
<strong><?php
$type = $product->get_auction_status();
switch ( $type ) {
case 'non-started':
echo esc_attr__( 'Not Started', 'yith-auctions-for-woocommerce' );
break;
case 'started':
echo esc_attr__( 'Started', 'yith-auctions-for-woocommerce' );
break;
case 'finished':
echo esc_attr__( 'Finished', 'yith-auctions-for-woocommerce' ) ;
break;
}
?>
</strong>
</li>
<li>
Auction Type <strong><?php echo $product->get_auction_type(); ?></strong>
</li>
<li>
Auction Start Date
<strong><?php
$dateinic = $product->get_start_date();
if ( $dateinic ) {
$format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
$format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
$format = $format_date . ' ' . $format_time;
$date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateinic ), $format );
echo $date;
}
?>
</strong>
</li>
<li>
Auction End Date
<strong><?php
$dateclose = $product->get_end_date();
if ( $dateclose ) {
$format_date = get_option( 'yith_wcact_general_date_format', 'j/n/Y' );
$format_time = get_option( 'yith_wcact_general_time_format', 'h:i:s' );
$format = $format_date . ' ' . $format_time;
$date = get_date_from_gmt( date( 'Y-m-d H:i:s', $dateclose ), $format );
echo $date;
}
?>
</strong>
</li>
</ul>
</div>
</div>
</div>
<?php
}
$html = ob_get_clean();
return $html;
}
}
add_shortcode( 'auction_information_field', 'auction_information_field_callback' );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342802.html
標籤:php WordPress的 求购 高级自定义字段 短代码
