嗨,有這個代碼可以在商店頁面上顯示多個類別的影像,我遇到的問題是,如果 AFC 影像只有一個,它仍然顯示一個空白點,而不僅僅是一個影像。我該如何修復它,以便如果沒有影像只顯示那里的影像?
add_action('woocommerce_before_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
$size = "shop_catalog";
$cat_img_1 = '<img src="'.get_field('category_image_1', $term_id).'"/>';
$cat_img_2 = '<img src="'.get_field('category_image_2', $term_id).'"/>';
$cat_img_3 = '<img src="'.get_field('category_image_3', $term_id).'"/>';
if (!empty($cat_img_1)&&empty($cat_img_2)&&empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo'</ol>';
}elseif (!empty($cat_img_1)&&!empty($cat_img_2)&&empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_2.'<div ></div></li>';
echo'</ol>';
}elseif (!empty($cat_img_1)&&!empty($cat_img_2)&&!empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_2.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_3.'<div ></div></li>';
echo'</ol>';
}
uj5u.com熱心網友回復:
正如我在評論中指出的那樣,讓您的圖片成為 array()
add_action('woocommerce_before_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
$slides = array();
if(get_field('category_image_1', $term_id)):
$slides[] .= '<img src="'.get_field('category_image_1', $term_id).'"/>';
endif;
if(get_field('category_image_2', $term_id)):
$slides[] .= '<img src="'.get_field('category_image_2', $term_id).'"/>';
endif;
if(get_field('category_image_3', $term_id)):
$slides[] .= '<img src="'.get_field('category_image_3', $term_id).'"/>';
endif;
if($slides):
echo '<ol >';
foreach($slides as $slide) {
echo '<li id="carousel__slide" tabindex="0" >'.$slide.'</li>';
}
echo '</ol>';
endif;
}
uj5u.com熱心網友回復:
如果您不想對代碼進行重大更改,請嘗試此方法
add_action('woocommerce_before_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
$size = "shop_catalog";
if( get_field('category_image_1', $term_id) ) {
$cat_img_1 = '<img src="'.get_field('category_image_1', $term_id).'"/>';
}
if( get_field('category_image_2', $term_id) ) {
$cat_img_2 = '<img src="'.get_field('category_image_2', $term_id).'"/>';
}
if( get_field('category_image_3', $term_id) ) {
$cat_img_3 = '<img src="'.get_field('category_image_3', $term_id).'"/>';
}
if (!empty($cat_img_1)&&empty($cat_img_2)&&empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo'</ol>';
}elseif (!empty($cat_img_1)&&!empty($cat_img_2)&&empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_2.'<div ></div></li>';
echo'</ol>';
}elseif (!empty($cat_img_1)&&!empty($cat_img_2)&&!empty($cat_img_3)) {
echo'<ol >';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_1.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_2.'<div ></div></li>';
echo '<li id="carousel__slide" tabindex="0" >'.$cat_img_3.'<div ></div></li>';
echo'</ol>';
}
}
而這里是一個更優化的版本,我把所有的圖片都加到陣列中,然后通過回圈來顯示圖片。
add_action('woocommerce_before_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
$size = "shop_catalog";
$images_array = array();
$images_array[] = get_field('category_image_1', $term_id);
$images_array[] = get_field('category_image_2', $term_id);
$images_array[] = get_field('category_image_3', $term_id);
$result_images_array = array_filter($array);
if(empty($result_images_array)) {
echo'<ol >';
foreach($result_images_array as $image_src) {
echo '<li id="carousel__slide" tabindex="0" >'.$image_src.'<div ></div></li>';
}
echo'</ol>';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/336509.html
標籤:php WordPress的 求购
