我正在为建筑师创建一个网站,大型图像是该网站的主要功能。考虑到图像的潜在大小,我想有一种方法来创建响应性的图像大小(当然我想到了css和背景图像。我想我可以使用css来构建小图像作为默认值,随着屏幕宽度的增加,使用媒体查询来添加更大的背景图像)。
我的想法是使用一个单独的动态样式表(sliders.php),编写一个脚本,在图像中循环创建幻灯片。
注意:我正在使用flexslider作为图像滑块,使用高级自定义字段和自定义帖子类型创建“建筑项目”,使用特色图像创建幻灯片并使用我的功能。php文件为背景图像自动创建多个大小。
前端的我的wordpress图像滑块是通过以下循环创建的:
<section class="slider slider-major" id="slider-major">
<?php
$the_query = new WP_Query(array(
\'post_type\' => \'projects\'
));
?>
<div class="RMA-slider slider-main slider-<?php echo the_ID(); ?>">
<ul class="slides">
<?php
$slideNumber = 0; // Set Slide numbering to 0
while (has_sub_field(\'project_slider_images\')) :
$slideNumber++;
?>
<li>
<div class="slider-image slide-<?php echo $slideNumber ?>">
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
</div>
</section>
运行了一些测试后,我很确定问题出在滑块上。php文件,仅用于设置滑块。我有两个样式表:
屏幕。css(我的非动态样式表)样式。php-一个动态样式表,从wordpress数据库中提取图像,并将其作为背景图像放入动态样式表中问题是,循环并不是针对每个项目都运行,我不知道为什么?
我已经将样式表与动态样式表进行了排队,但没有更改。下面是整个动态样式表。我运行了3个不同的滑块,因此创建了3个滑块循环:
<?php
header(\'Content-type: text/css\');
header("Cache-Control: must-revalidate");
$offset = 100;
$ExpStr = "Expires" . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
$parse_uri = explode( \'wp-content\', $_SERVER[\'SCRIPT_FILENAME\'] );
$url = $parse_uri[0];
require_once($url . \'wp-load.php\' );
require_once($url . \'wp-includes/post.php\');
require_once($url . \'wp-content/plugins/advanced-custom-fields/acf.php\');
require_once($url . \'wp-content/plugins/custom-post-type-ui/custom-post-type-ui.php\');
?>
/* ___________MAIN SLIDER IMAGE SET-UP______________________________________________ */
//THIS WORKS AS IT SHOULD
<?php
$the_query = new WP_Query(array(
\'post_type\' => \'slides\',
\'posts_per_page\' => -1
));
$sliderTitle = get_the_title();
$slideNumber = 0;
while ($the_query->have_posts()) :
$the_query->the_post();
$slideNumber++;
$attachment_id = get_field(\'slider_image\');
$sizeSm = "slider-image-sm";
$sizeMd = "slider-image-md";
$sizeLg = "slider-image-Lg";
$image_attributes_sm = wp_get_attachment_image_src( $attachment_id, $sizeSm );
$image_attributes_md = wp_get_attachment_image_src( $attachment_id, $sizeMd );
$image_attributes_lg = wp_get_attachment_image_src( $attachment_id, $sizeLg );
?>
// Set-up Responsive Image loading and allow for full-page background image slider
.slider-major .slider-main.slider-homepage .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_sm[0]; ?>");
}
@media only screen and (min-height: 45em) {
.slider-major .slider-main.slider-homepage .slide-<?php echo $slideNumber; ?>{
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 45em) {
.slider-major .slider-main.slider-homepage .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 100em) {
.slider-major .slider-main.slider-homepage .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_lg[0]; ?>");
}
}
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
/* ___________PROJECT SLIDER IMAGE SET-UP________________________________________ */
//THIS IS THE PROBLEM. ONLY LOOPS THROUGH A FEW PROJECTS. THERE WILL BE UPWARDS OF 15 PROJECTS ON THE SITE AND ONLY 4-5 OF THEM WORK TO SHOW IMAGES
<?php
$the_query = new WP_Query(array(
\'post_type\' => \'projects\'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
$slideNumber = 0;
while (has_sub_field(\'project_slider_images\')) :
$slideNumber++;
$attachment_id = get_sub_field(\'project_slider_image\');
$sizeSm = "slider-image-sm";
$sizeMd = "slider-image-md";
$sizeLg = "slider-image-Lg";
$image_attributes_sm = wp_get_attachment_image_src( $attachment_id, $sizeSm );
$image_attributes_md = wp_get_attachment_image_src( $attachment_id, $sizeMd );
$image_attributes_lg = wp_get_attachment_image_src( $attachment_id, $sizeLg );
?>
// Set-up Responsive Image loading and allow for full-page background image slider
.slider-major .slider-main.slider-<?php echo the_ID(); ?> .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_sm[0]; ?>");
}
@media only screen and (min-height: 45em) {
.slider-major .slider-main.slider-<?php echo the_ID(); ?> .slide-<?php echo $slideNumber; ?>{
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 45em) {
.slider-major .slider-main.slider-<?php echo the_ID(); ?> .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 100em) {
.slider-major .slider-main.slider-<?php echo the_ID(); ?> .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_lg[0]; ?>");
}
}
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endwhile; ?>
/* ___________PRACTICE SLIDER IMAGE SET-UP____________________________________ */
//THIS WORKS AS IT SHOULD
<?php
$the_query = new WP_Query(array(
\'post_type\' => \'practice_slides\',
\'posts_per_page\' => -1
));
$sliderTitle = get_the_title();
$slideNumber = 0;
while ($the_query->have_posts()) :
$the_query->the_post();
$slideNumber++;
$attachment_id = get_field(\'practice_slider_image\');
$sizeSm = "slider-image-sm";
$sizeMd = "slider-image-md";
$sizeLg = "slider-image-Lg";
$image_attributes_sm = wp_get_attachment_image_src( $attachment_id, $sizeSm );
$image_attributes_md = wp_get_attachment_image_src( $attachment_id, $sizeMd );
$image_attributes_lg = wp_get_attachment_image_src( $attachment_id, $sizeLg );
?>
// Set-up Responsive Image loading and allow for full-page background image slider
.slider-major .slider-main.slider-practice .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_sm[0]; ?>");
}
@media only screen and (min-height: 45em) {
.slider-major .slider-main.slider-practice .slide-<?php echo $slideNumber; ?>{
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 45em) {
.slider-major .slider-main.slider-practice .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_md[0]; ?>");
}
}
@media only screen and (min-width: 100em) {
.slider-major .slider-main.slider-practice .slide-<?php echo $slideNumber; ?> {
background-image: url("<?php echo $image_attributes_lg[0]; ?>");
}
}
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>