【发布时间】:2014-09-26 04:05:12
【问题描述】:
我目前的主页滑块图像源设置如下:src="<?php the_field('slider_foto'); ?>",这在我的 front-page.php 中效果很好。
这些图像的尺寸为 1600x500 像素,这对于大屏幕来说非常有用,但是一旦低于 767 像素视口尺寸,滑块的宽度就会比屏幕尺寸小。
因此,一旦视口宽度低于 767 像素,我希望图像源更改为 src="<?php the_field('slider_foto_mobile'); ?>"。我发现这篇文章给了我一个开始:Change image source based on viewport width
但我不知道如何为函数中的图像 src 实现预定义的<?php the_field(); ?>。
有人可以帮我解决这个问题吗?
谢谢!
编辑 1
我发现我将使用 Mobile Detect 来解决这个问题,但是我在实现它时遇到了问题。我将 php 文件复制到主题的根目录,接下来我需要做什么?我是否需要将其包含在我的functions.php中:
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
我坚持要做什么,我不明白。
我是否也需要在functions.php中调用这样的东西? $detect->isMobile();
$detect->isTablet();
然后在我的front-page.php中使用它? if ($detect->isMobile()) {}?
谢谢! /EDIT 1
编辑 2
我现在有了这是我的front-page.php,因为那是我唯一使用它的地方。我的文件是这样开始的:
<?php get_header(); ?>
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
?>
在页面下方一点我执行以下操作:
<?php if ( $detect->isMobile() && $detect->isTablet() ) { ?>
<img class="carousel-image" src="<?php the_field('slider_foto_mobile'); ?>" alt="<?php the_title(); ?>">
<?php } else { ?>
<img class="carousel-image" src="<?php the_field('slider_foto'); ?>" alt="<?php the_title(); ?>">
<?php } ?>
我在 iPad 上工作,它区分了 src,但在 iPhone(可能还有其他所有手机)上,它仍然显示 <?php the_field('slider_foto'); ?> 而不是 <?php the_field('slider_foto_mobile'); ?>
同样,它适用于平板电脑!什么问题?
谢谢!
/EDIT 2
【问题讨论】:
标签: php