有没有办法在wordpress中的所有页面添加一个元素

时间:2016-12-14 08:47:16

标签: wordpress

所以我在wordpress +和主索引模板上有一些自定义创建的页面,这是我的主页面。我想在所有页面上添加滚动到顶部按钮。当我向主索引页面添加代码时,它只在主页上显示当我将它添加到页面模板文件时,它只出现在帖子页面上。所以有一种方法可以使代码在两个页面上运行(我有4个自定义页面,只是给出了2个示例),而无需复制每个文件上的代码?因为如果我制作更多的自定义页面,那将会复制很多代码,而且我绝对不希望如此。谢谢:P

3 个答案:

答案 0 :(得分:1)

嗨,你试过使用这个插件吗? https://wordpress.org/plugins/scroll-to-top-button/

答案 1 :(得分:1)

使用滚动创建按钮的短代码,并在所有页面的前端添加短代码

答案 2 :(得分:0)

所有页面(normaly)都使用

footer.php header.php 。你可以把这些代码放在那里。

实现“滚动到顶部按钮”的最佳方式是通过javascript进行IMO。 javascript代码可以检查“滚动到顶部按钮”是否合适,因为当内容不是很大时,或者如果你真的在顶部时你不需要它。当您向下滚动一定距离时,它会出现在固定位置。

示例(使用jQuery):

var scroll_top_button_is_visible = false;
var scroll_distance = 500;

var $scroll_top_button = $('<div class="scroll-top-button">back to top</div>');
// inject the button, so you don't have to create the button in any templates
$scroll_top_button.appendTo('body');

$(window).scroll(function() {
  // called on every scroll action
  if ($(window).scrollTop() > scroll_distance && !scroll_top_button_is_visible) {
    scroll_top_button_is_visible = true;
    $scroll_top_button.fadeIn(200);
  } else if ($(window).scrollTop() < scroll_distance && scroll_top_button_is_visible) {
    scroll_top_button_is_visible = false;
    $scroll_top_button.fadeOut(200);
  }
});

// scroll to top if button is clicked
$('.scroll-top-button').click(function() {
  $("html, body").animate({
    scrollTop: 0
  }, "slow");
});
p {
  margin-bottom: 300px;
}

.scroll-top-button {
  display: none;
  position: fixed;
  bottom: 10px;
  right: 10px;
  padding: 5px;
  color: white;
  background: red;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="page">
  <h1>Example of a "Scroll to Top Button"</h1>
  <h2>Scroll down and look what happens</h2>
  <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
   <p>Some content</p>
</div>