【发布时间】:2016-11-03 23:59:14
【问题描述】:
我正在尝试在子主题中制作自定义 WordPress 循环,以便可以将视频添加到主页的背景。我已将模板添加到我需要使用的页面的子主题中。
问题是其他几个页面将使用相同的模板,所以我需要自定义循环,以便它只在首页显示视频,但仍然使用模板来显示其余内容。
这是我目前拥有的带有自定义循环的自定义模板;
<?php
get_header(); ?>
<?php if (is_front_page()): ?>
<div class="fullscreen-bg">
<video loop muted autoplay poster="wp-content/uploads/2016/10/I_Waited_VA_Logo.png" class="fullscreen-bg__video">
<source src="wp-content/uploads/2016/07/I_Watied_VA_Video_1_Long_01.webm" type="video/webm">
<source src="wp-content/uploads/2016/07/I-Watied-VA-Video-1-Long.mp4" type="video/mp4">
</video>
</div>
<?php endif; ?>
<div id="your-content">
<?php
while ( have_posts() ) :
the_post();
the_content();
endwhile;
?>
</div>
<?php
get_footer();
?>
这会显示视频,但无法正常显示。它也不适用于使用该模板的其他页面。
这是我在 style.css 中使用的 CSS;
@import url("../campaign/style.css");
/* Full screen video*/
.fullscreen-bg {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
z-index: -100;
}
.fullscreen-bg__video {
position: absolute;
top: 50%;
left: 50%;
width: auto;
height: auto;
min-width: 100%;
min-height: 100%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
@media (max-width: 767px) {
.fullscreen-bg {
background: url('wp-content/uploads/2016/10/I_Waited_VA_Logo.png') center center / cover no-repeat;
}
.fullscreen-bg__video {
display: none;
}
}
#your-content {
position: relative;
z-index: 1;
}
【问题讨论】:
-
使用WP模板系统,您应该以主页模板与其他页面模板不同的方式进行此操作。此外,要检测它是否是主页,请使用 WP 函数
is_home()和/或is_front_page()