【问题标题】:Adding a clickable link to a background image slider将可点击链接添加到背景图像滑块
【发布时间】:2020-08-15 20:33:21
【问题描述】:

如果这个问题已经得到回答,我提前道歉。

我是php和css的新手(可以和html相处)。在 Google 和各种论坛上花费了相当多的时间之后,到目前为止,我一直在努力尝试让它工作,但没有运气。

我的目标是让主页滑块 (http://etractorimplements.com/) 上的背景图片可点击链接。

代码如下:

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<!-- Item 1 -->
<?php               
$args = array('posts_per_page' => '-1','category' =>22, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post');                
$recent = get_posts($args);             
$count = 0;                 
$i=1;               
foreach( $recent as $post ){                
$image_url= wp_get_attachment_url( get_post_thumbnail_id($post->ID) );          ?>
<div class="item lazy slide<?php echo $i;?> <?php echo ($count == 0) ? 'active' : ''; ?>" role="list">
<div class="row">
<div class="container">
<div class="col-md-7 text-left my_res1">
<h3 data-animation="animated fadeIn"> <?php echo $post->post_title;?></h3>
<h4 data-animation="animated fadeInUp"> <?php echo $post->post_content;?></h4> </div>
<div class="col-md-5 text-right my_res2">
<!--<img  data-animation="animated bounceInUp" src="img/a.png"> --></div>
</div>
</div>
</div>
<style>
#first-slider .slide<?php echo $i;
?> {
background-image: url(<?php echo $image_url;?>);
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
</style>\

我希望四个滑块中的每一个都链接到相应的类别页面(即幻灯片:1 - Ditch Flail Mower 链接到“Flail Mowers”类别页面)

当我检查页面源时,我可以看到相应的 html 代码:

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<!-- Item 1 -->
<div class="item lazy slide1 active" role="list">
<div class="row">
<div class="container">
<div class="col-md-7 text-left my_res1">
<h3 data-animation="animated fadeIn"> Ditch Flail Mower</h3>
<h4 data-animation="animated fadeInUp"> Ideal for your small to mid-sized tractor
providing maximum flexibility</h4> </div>
<div class="col-md-5 text-right my_res2">
<!--<img  data-animation="animated bounceInUp" src="img/a.png"> --></div>
</div>
</div>
</div>
<style>
#first-slider .slide1 {
background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/flailmower.jpg);
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
</style>
<div class="item lazy slide2 " role="list">
<div class="row">
<div class="container">
<div class="col-md-7 text-left my_res1">
<h3 data-animation="animated fadeIn"> Wood Chipper</h3>
<h4 data-animation="animated fadeInUp"> Powerful &amp; Reliable
Easy to Use</h4> </div>
<div class="col-md-5 text-right my_res2">
<!--<img  data-animation="animated bounceInUp" src="img/a.png"> --></div>
</div>
</div>
</div>
<style>
#first-slider .slide2 {
background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/sl2-compressor.jpg);
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
</style>
<div class="item lazy slide3 " role="list">
<div class="row">
<div class="container">
<div class="col-md-7 text-left my_res1">
<h3 data-animation="animated fadeIn"> Rotary Tiller</h3>
<h4 data-animation="animated fadeInUp"> To make your work easy,
the right size to fit your tractor.</h4> </div>
<div class="col-md-5 text-right my_res2">
<!--<img  data-animation="animated bounceInUp" src="img/a.png"> --></div>
</div>
</div>
</div>
<style>
#first-slider .slide3 {
background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/sl4-compressor.jpg);
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
</style>
<div class="item lazy slide4 " role="list">
<div class="row">
<div class="container">
<div class="col-md-7 text-left my_res1">
<h3 data-animation="animated fadeIn"> Snow Plow</h3>
<h4 data-animation="animated fadeInUp"> Innovative &amp; reliable plow that removes
snow even in the worst conditions</h4> </div>
<div class="col-md-5 text-right my_res2">
<!--<img  data-animation="animated bounceInUp" src="img/a.png"> --></div>
</div>
</div>
</div>
<style>
#first-slider .slide4 {
background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/snow-banner.jpg);
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
</style>
</div>
<!-- End Wrapper for slides-->

谁能帮我找出插入“frontpage.php”文件的正确代码?

提前致谢!

拉比安锥

【问题讨论】:

  • 请点击编辑。然后 [&lt;&gt;] sn-p 编辑器并发布一个 minimal reproducible example 的 JUST 相关 HTML、CSS 和脚本。无需 PHP
  • 您的代码中没有要重定向的 href 或脚本
  • 如果您需要我们的帮助,您需要提供minimal reproducible example。您的网站已缩小,因此找不到有用的 HTML 或脚本
  • @mplungjan 首先,我要感谢您帮助我解决这个问题。据我所知,我用来缩小的插件是 WP Rocket。我试图通过取消选中插件设置中的框来消除缩小,但我所知道的代码中没有任何变化。看来我只能访问 .php 和 .css 文件,但不能访问 .html。

标签: javascript php html jquery css


【解决方案1】:

将此代码放在&lt;script&gt;&lt;/script&gt;中的代码中(头部)您的页面

const links = {
  slide1: "https://etractorimplements.com/product-category/flail-mowers/",
  slide2: "https://etractorimplements.com/product-category/wood-chippers/",
  slide3: "https://etractorimplements.com/product-category/rotary-tillers/",
  slide4: "https://etractorimplements.com/product-category/winter-equipments/snowplow/"
}; // list of links

$(function() {
  $(".carousel-inner").css("cursor","pointer"); // or add .carousel-inner { cursor: pointer; } to the end of your CSS file
  $(".carousel-inner").on("click tap touchstart", ".item", function() {
    const slide = $(this).attr("class").split(/\s+/).filter(cls => cls.startsWith("slide"))
    if (slide.length === 1) location = links[slide[0]];
  });
});

例子

const links = {
  slide1: "https://etractorimplements.com/product-category/flail-mowers/",
  slide2: "https://etractorimplements.com/product-category/wood-chippers/",
  slide3: "https://etractorimplements.com/product-category/rotary-tillers/",
  slide4: "https://etractorimplements.com/product-category/winter-equipments/snowplow/"
}

$(function() {
  $(".carousel-inner").css("cursor","pointer");
  $(".carousel-inner").on("click tap touchstart", ".item", function() {
    const slide = $(this).attr("class").split(/\s+/).filter(cls => cls.startsWith("slide"))
    if (slide.length === 1) location = links[slide[0]];
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js" integrity="sha512-bPs7Ae6pVvhOSiIcyUClR7/q2OAsRiovw4vAkX+zJbw3ShAeeqezq50RIIcIURq7Oa20rW2n2q+fyXBNcU9lrw==" crossorigin="anonymous"></script>
<div id="first-slider">
  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carousel-example-generic" data-slide-to="0" class=""></li>
      <li data-target="#carousel-example-generic" data-slide-to="1" class="active"></li>
      <li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
      <li data-target="#carousel-example-generic" data-slide-to="3" class=""></li>
    </ol>
    <div class="carousel-inner" role="listbox">
      <div class="item slide1" role="list">
        <div class="row">
          <div class="container">
            <div class="col-md-7 text-left my_res1">
              <h3 data-animation="animated fadeIn"> Ditch Flail Mower</h3>
              <h4 data-animation="animated fadeInUp"> Ideal for your small to mid-sized tractor providing maximum flexibility</h4>
            </div>
            <a href="https://etractorimplements.com/product-category/flail-mowers/"></a>
            <div class="col-md-5 text-right my_res2"></div>
          </div>
        </div>
      </div>
      <style>
        #first-slider .slide1 {
          background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/flailmower.jpg);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center
        }
      </style>
      <div class="item slide2 active" role="list">
        <div class="row">
          <div class="container">
            <div class="col-md-7 text-left my_res1">
              <h3 data-animation="animated fadeIn"> Wood Chipper</h3>
              <h4 data-animation="animated fadeInUp"> Powerful &amp; Reliable Easy to Use</h4>
            </div>
            <a href="https://etractorimplements.com/product-category/flail-mowers/"></a>
            <div class="col-md-5 text-right my_res2"></div>
          </div>
        </div>
      </div>
      <style>
        #first-slider .slide2 {
          background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/sl2-compressor.jpg);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center
        }
      </style>
      <div class="item slide3" role="list">
        <div class="row">
          <div class="container">
            <div class="col-md-7 text-left my_res1">
              <h3 data-animation="animated fadeIn"> Rotary Tiller</h3>
              <h4 data-animation="animated fadeInUp"> To make your work easy, the right size to fit your tractor.</h4>
            </div>
            <a href="https://etractorimplements.com/product-category/flail-mowers/"></a>
            <div class="col-md-5 text-right my_res2"></div>
          </div>
        </div>
      </div>
      <style>
        #first-slider .slide3 {
          background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/sl4-compressor.jpg);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center
        }
      </style>
      <div class="item slide4" role="list">
        <div class="row">
          <div class="container">
            <div class="col-md-7 text-left my_res1">
              <h3 data-animation="animated fadeIn"> Snow Plow</h3>
              <h4 data-animation="animated fadeInUp"> Innovative &amp; reliable plow that removes snow even in the worst conditions</h4>
            </div>
            <a href="https://etractorimplements.com/product-category/flail-mowers/"></a>
            <div class="col-md-5 text-right my_res2"></div>
          </div>
        </div>
      </div>
      <style>
        #first-slider .slide4 {
          background-image: url(https://etractorimplements.com/wp-content/uploads/2017/10/snow-banner.jpg);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center
        }
      </style>
    </div>
    <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> <i class="fa fa-angle-left"></i><span class="sr-only">Previous</span> </a>
    <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> <i class="fa fa-angle-right"></i><span class="sr-only">Next</span> </a>
  </div>
</div>

【讨论】:

  • 控制主页的文件是“frontpage.php”,不包含标签。我把上面的代码 sn -p 粘贴到文件的开头,但是主页上只出现了一长串文本。 “header.php”包含一个 标签。我将提供的代码复制到“header.php”上的 标记中,得到了相同的结果。很抱歉,我在解释正在发生的事情方面做得很差,无法为您提供必要的信息来帮助我。您还有其他想法吗?
  • 代码需要被包裹在一个标签中
  • 谢谢您,先生。那成功了。当然,现在我有更多的问题,大声笑。我假设当图像变成可点击的链接时,光标会在悬停时变成指针,表明它是一个链接,但它仍然是一个箭头。如果可能的话,我想用指针在悬停时显示。此外,这些链接似乎不适用于移动设备(在 safari 和 chrome 应用程序上测试),只能在桌面设备上使用。你知道是否有办法解决这个问题,或者它可能是代码的性质?无论哪种方式,我都会永远感谢您的帮助。
  • 或添加到您的 CSS:.carousel-inner { cursor: pointer; }.
  • @kmoser 如果能找到不是插件生成的
猜你喜欢
  • 2015-11-18
  • 2017-08-28
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
相关资源
最近更新 更多