function media_display_shortcode($atts) {
$atts = shortcode_atts(
array(
'tag' => '',
'limit' => -1,
'type' => 'all', // video, image, all (پیشفرض all)
'layout' => 'default' // default, carousel, grid
),
$atts,
'media_display'
);
$tags = array();
if (trim($atts['tag']) === 'me' && function_exists('is_product') && is_product()) {
global $product;
if ($product) {
$sku = $product->get_sku();
$model = $product->get_attribute('pa_model');
if (!empty($sku)) {
$tags[] = strval($sku);
}
if (!empty($model)) {
$tags[] = strval($model);
}
}
} elseif (strpos($atts['tag'], 'me') !== false) {
return '';
} else {
$tags = explode(',', $atts['tag']);
$tags = array_map('trim', $tags);
}
$meta_query = array('relation' => 'OR');
foreach ($tags as $tag) {
$meta_query[] = array(
'key' => '_media_tags',
'value' => $tag,
'compare' => 'LIKE'
);
}
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => intval($atts['limit']),
'meta_query' => $meta_query
);
$query = new WP_Query($args);
// آرایه ها برای دسته بندی
$videos = [];
$audios = [];
$images = [];
$seen_ids = [];
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
if (in_array($post_id, $seen_ids)) continue;
$seen_ids[] = $post_id;
$mime_type = get_post_mime_type($post_id);
$media_url = wp_get_attachment_url($post_id);
$thumbnail_html = wp_get_attachment_image($post_id, 'large');
$media_item = '
';
$videos[] = $media_item;
} elseif (strpos($mime_type, 'audio') !== false) {
$media_item .= '';
$media_item .= ''. esc_html(get_post_field('post_content', $post_id)).'';
$media_item .= '';
$audios[] = $media_item;
} elseif (strpos($mime_type, 'image') !== false) {
$image_id = get_post_thumbnail_id($post_id);
$image_url = wp_get_attachment_url($image_id);
$media_item .= ''.$thumbnail_html.'';
$media_item .= ''. esc_html(get_post_field('post_content', $post_id)).'';
$media_item .= '';
$images[] = $media_item;
}
}
wp_reset_postdata();
// ترکیب آرایهها بر اساس نوع درخواست
$output_items = [];
if ($atts['type'] === 'video') {
$output_items = $videos;
} elseif ($atts['type'] === 'image') {
$output_items = $images;
} else {
$output_items = array_merge($videos, $images);
}
if (empty($output_items)) return '';
// خروجی HTML با توجه به layout
ob_start();
// لود CSS و JS فنسباکس فقط یکبار و اگر لازم بود
echo '
';
// استایل پایه (برای grid و default)
echo '';
// اگر حالت carousel خواستی اینجا لود کن (مثلاً Slick Carousel)
if ($atts['layout'] === 'carousel') {
// لود CSS و JS اسلایدر (مثال: Slick)
echo '
';
}
// کلاس wrapper بر اساس layout
$gallery_class = 'media-gallery';
if ($atts['layout'] === 'carousel') {
$gallery_class .= ' carousel';
} elseif ($atts['layout'] === 'grid') {
$gallery_class .= ' grid';
} else {
$gallery_class .= ' default';
}
echo '';
// فنسباکس JS
echo '';
return ob_get_clean();
}
add_shortcode('media_display', 'media_display_shortcode');