在过去的几个小时里,这已经让我感到困惑。希望任何人都有明确的答案,无论如何,感谢您的支持。需要在 jQuery 中设置的数据属性中的三个值之间进行过滤。这些值会根据 进行更改并放入属性中screenwidth
。jQuery 部分工作正常。现在棘手的部分是,在 PHP 文件中,应该使用 data-width 属性的正确值并将其作为自定义字段的值。该值会触发backgroundslider
.
我尝试了 if/else 语句,其中属性值有一个变量,custumfield
当它的值与 jQuery 中的 mad 值相等时,它可以为 . 这失败了。我的知识有限,因此我的文件看起来像这样。
jQuery:
function schermFilter() {
var scherm = $(window).width();
if (scherm >= 320 && scherm <= 480) {
$('#homeslider-wrapper').attr('data-width','slides_mobiel')
console.log(scherm + ": mobiel");
} else if (scherm >= 768 && scherm <= 992) {
$('#homeslider-wrapper').attr('data-width','slides_tablet')
console.log(scherm + ": tablet");
} else {
$('#homeslider-wrapper').attr('data-width','slides_groot')
console.log(scherm + ": groot");
}
};
schermFilter();
$(document).ready(function() {
schermFilter();
});
PHP:
<?php $screenwidth =""; ?>
<?php $args = array(
'post_type' => 'homeslides',
'order' => 'date'
);
$slider = new WP_QUERY($args);
if($slider->have_posts()) : ?>
<div id="homeslider-wrapper" class="slides" data-width="$screenwidth">
<?php while ($slider->have_posts()) : $slider->the_post(); ?>
<?php if ($screenwidth === "slides_mobiel") {
$image = get_field('slides_mobiel');
echo $screenwidth;
} elseif ($screenwidth === "slides_tablet") {
$image = get_field('slides_tablet');
echo $screenwidth;
} else if ($screenwidth === "") {
$image = get_field('slides_groot');
echo $screenwidth;
} else {
$image = get_field('slides_groot');
echo "groot - else";
}
?>
<?php $imageList .= '"'. $image['url'] . '",'; ?>
<?php endwhile; ?>
</div>
<?php endif;wp_reset_postdata(); ?>
<?php $imageList = rtrim($imageList, ','); ?>
我试过isset
and property-exists
,但没有成功。谢谢!