Я был озадачен этим странным поведением в течение последних двух часов. Я понятия не имею, откуда это взялось, и отладка ни к чему меня не привела.
Код: Выделить всё
function custom_gallery_get_images($atts) {
// Extract attributes and set default values
$atts = shortcode_atts(array(
'id' => '',
'image_index' => 0,
'image_count' => 0,
'shift' => 0,
'overflow' => 1,
), $atts);
error_log(print_r($atts, true));
if (!$atts['id']) {
return 'Gallery not found.';
}
$image_ids = get_post_meta($atts['id'], '_custom_gallery_images', true);
if (!$image_ids) {
return 'No images found in this gallery.';
}
$image_index = intval($atts['image_index']);
$image_count = intval($atts['image_count']);
$shift = intval($atts['shift']);
$overflow = filter_var($atts['overflow'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); // Convert to boolean
$overflow = false;
error_log(print_r( $overflow . " 1", true));
error_log(print_r( false . " test", true ));
if (is_null($overflow)) {
$overflow = "NULL";
}
error_log(print_r( $overflow . " 2", true));
// Ensure images are an array
$image_ids = explode(',', $image_ids);
$total_images = count($image_ids);
// Error handling if the requested count is larger than available images
if ($image_count > $total_images) {
return 'Error: Requested image count exceeds available images in the gallery.';
}
// Calculate the start index
$start_index = ($image_index - $shift) % $total_images; // Use modulo for wrapping
// Handle negative indices for wrapping around
if ($start_index < 0) {
$start_index += $total_images; // Shift the index into positive range
}
// Get the desired images, considering the wrap-around
$selected_images = [];
for ($i = 0; $i < $image_count; $i++) {
// Calculate the index with wrapping
$current_index = ($start_index + $i) % $total_images;
$selected_images[] = $image_ids[$current_index];
}
// Build the output HTML
$output = '';
$count = 0;
foreach ($selected_images as $image_id) {
error_log(print_r( $overflow, true));
if(!$overflow && $image_id < $start_index) {
$hmm = "false";
error_log(print_r( $overflow . " 3", true));
$one = $overflow;
$two = $image_id;
$three = $start_index;
continue;
} else {
$hmm = "true";
error_log(print_r( $overflow . " 4", true));
$one = $overflow;
$two = $image_id;
$three = $start_index;
}
$img_url = wp_get_attachment_url(trim($image_id));
$img_url_trimmed = rtrim($img_url, '/');
if ($img_url_trimmed) {
$output .= '
[img] . $img_url . [/img]
';
}
$count++;
}
$output .= '';
return $output;
}
Основная проблема связана со значением overflow, которое должно быть простым логическим флагом, который будет использоваться в такой короткий код:
[custom_gallery_images id="223" image_index=0 image_count=10 overflow=0]
или этот
[custom_gallery_images id="223" image_index=0 image_count=10 overflow ='false']
или это
[custom_gallery_images id="223" image_index=0 image_count=10 overflow=false]
все, кроме этого значения, уже реализовано и работал раньше.
Код: Выделить всё
\[10-Oct-2024 12:24:19 UTC\] Array
(
\[id\] =\> 223
\[image_index\] =\> 0
\[image_count\] =\> 10
\[shift\] =\> 0
\[overflow\] =\> false
)
\[10-Oct-2024 12:24:19 UTC\] 1
\[10-Oct-2024 12:24:19 UTC\] test
\[10-Oct-2024 12:24:19 UTC\] 2
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
\[10-Oct-2024 12:24:19 UTC\]
\[10-Oct-2024 12:24:19 UTC\] 4
Почему всегда '', а не '0' ? Для меня это не имеет смысла, и я не понимаю такого поведения.
Когда я устанавливаю для него значение true, он работает нормально и выводит все 1.
'' не является даже интерпретируется как ложное оператором if, который находится далее в коде.
Я пытался заставить его быть ложным. Запрашиваю в чат-gpt несколько способов кастинга, много console.logов, но ничего не меняется.
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-always-1