使用Codestar Framework框架创建的图片相册调用及设置相关记录

1,572次阅读
没有评论

gallery(使用画廊属性) 后台框架代码

if (class_exists('CSF')) {
    $prefix_post_opts = 'ceo_post_opts';
    CSF::createMetabox($prefix_post_opts, array(
        'title' => '产品信息设置',
        'post_type' => 'post',
        'data_type' => 'unserialize',
        'priority' => 'high',
    ));

    CSF::createSection($prefix_post_opts, array(
        'name' => 'post_layout_box',
        'title' => '布局设置',
        'icon' => 'iconfont icon-buju',
        'fields' => array(
            array(
                'id' => 'single_post_style',
                'type' => 'button_set',
                'title' => '文章样式',
                'options' => array(
                    'single-news' => '<i class="fas fa-newspaper"></i> 新闻',
                    'single-grid' => '<i class="fas fa-th"></i> 产品',
                ),
                'default' => 'single-news',
            ),
            array(
                'type' => 'notice',
                'style' => 'warning',
                'content' => '产品参数',
                'dependency' => array('single_post_style', '==', 'single-grid'),
            ),
            // array(
            //     'id' => 'anli_z',
            //     'type' => 'repeater',
            //     'title' => '产品相册',
            //     'fields' => array(
            //         array(
            //             'id' => 'title',
            //             'type' => 'upload',
            //             'title' => '上传图片',
            //         ),
            //     ),
            //     'dependency' => array('single_post_style', '==', 'single-grid'),
            // ),
			
			array(
			    'id' => 'anli_z',
			    'type' => 'gallery',
			    'title' => '产品相册',
			    'desc' => '一次性上传多张图片',
			    'dependency' => array('single_post_style', '==', 'single-grid'),
			)

			
        ),
    ));
}

调用方法:

例子1:根据官方给你出的代码:

<?php
$gallery_opt = get_post_meta(get_the_ID(), 'anli_z', true);
$gallery_ids = explode(',', $gallery_opt); // 将字符串分割成数组

if (!empty($gallery_ids)) {
    foreach ($gallery_ids as $gallery_item_id) {
        echo wp_get_attachment_image($gallery_item_id, 'full'); // 显示完整尺寸的图片
        // echo wp_get_attachment_url($gallery_item_id); // 获取图片的URL
        // echo wp_get_attachment_image_src($gallery_item_id, 'full'); // 获取图片的尺寸信息
        // var_dump(wp_get_attachment_metadata($gallery_item_id)); // 获取附件的元数据
    }
}
?>

例子2:请将这段代码插入到你的HTML代码中,确保在适当的位置插入此代码。这段代码将遍历每个图库项,并输出对应的HTML格式,其中包含一个链接(<a>标签)和图像(<img>标签),链接的目标为图片的地址。

<?php
$gallery_opt = get_post_meta(get_the_ID(), 'anli_z', true);
$gallery_ids = explode(',', $gallery_opt); 

if (!empty($gallery_ids)) {
    foreach ($gallery_ids as $gallery_item_id) {
        $image_url = wp_get_attachment_url($gallery_item_id);
        echo '<a href="' . $image_url . '">';
        echo '<img src="' . $image_url . '" />';
        echo '</a>';
    }
}
?>

例子3:如果用做产品详情页,调用 我们还有产品特色图+产品相册,那我们需要一起输出。再做个判断,如果没有添加产品图库,只有特色图,那么我们只显示特色图。废话不说直接上代码

<?php
						$anli_z = get_post_meta(get_the_ID(), 'anli_z', true);

						if (empty($anli_z)) {
							echo '<div class="sp-loading2"><br>';
							echo '<img id="product_detail_img" src="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1" />';
							echo '</div>';
						} else {
							echo '<div class="sp-wrap">';
							echo '<a href="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1">';
							echo '<img id="product_detail_img" src="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1" />';
							echo '</a>';

							$gallery_opt = get_post_meta(get_the_ID(), 'anli_z', true);
							$gallery_ids = explode(',', $gallery_opt); 
							
							if (!empty($gallery_ids)) {
							    foreach ($gallery_ids as $gallery_item_id) {
							        $image_url = wp_get_attachment_url($gallery_item_id);
							        echo '<a href="' . $image_url . '">';
							        echo '<img src="' . $image_url . '" />';
							        echo '</a>';
							    }
							}

							echo '</div>';
						}
						?>

关闭gallery(画廊属性)采用:upload 统一的功能。

						<?php
						$anli_z = get_post_meta(get_the_ID(), 'anli_z', true);

						if (empty($anli_z)) {
							echo '<div class="sp-loading2"><br>';
							echo '<img id="product_detail_img" src="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1" />';
							echo '</div>';
						} else {
							echo '<div class="sp-wrap">';
							echo '<a href="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1">';
							echo '<img id="product_detail_img" src="' . get_template_directory_uri() . '/timthumb.php?src=' . post_thumbnail_src() . '&h=500&w=500&zc=1" />';
							echo '</a>';

							foreach ($anli_z as $k => $v) {
								$number = $k + 1;
								echo '<a href="' . $v['title'] . '">';
								echo '<img src="' . $v['title'] . '" />';
								echo '</a>';
							}

							echo '</div>';
						}
						?>

 

这里我从项目里面直接摘录出来,用在别的主题,还需要自己修改。
仿站定制,请联系我们。

正文完
 0
评论(没有评论)

觅站网

Posts

Sed cras ornare arcu dui vivamus arcu felis. Cursus euismod quis viverra nibh cras pulvinar mattis nunc.

Melissa Meiers