在WordPress中构建向下滑动搜索框

时间:2017-06-12 作者:user7548188

我正在构建一个向下滑动的搜索框。我已经成功地编写了它,它在JSFIDLE上也能正常工作。然而,当我试图在wordpress上实现它时,它并不起作用。因为它在JSFIDLE上工作,所以它必须在wordpress上工作,但我不知道问题出在哪里。有人看到任何错误吗?

JSFIDLE-https://jsfiddle.net/jd0a433f/1/

searchform。php

<div class="search-field">
</div>
<form role="search" method="get" class="search-form" action="<?php echo home_url( \'/\' ); ?>">
  <label for="search-input" id="search-label" class="search-label">
    Search for:   
    </label>
<input id="search-input" type="search" class="search-input" placeholder="Search …" value="" name="s" title="Search for:" />
  <input type="submit" class="search-submit" value="Search" />
</form>
搜索js公司

document.getElementById("search-label").addEventListener("click", function(e) {
  if (e.target == this) {
    e.preventDefault();
    this.classList.toggle("clicked");
  }
});
功能。php

function searchfunction() {
wp_enqueue_script( \'my-script\', get_stylesheet_directory_uri() . \'/js/search.js\', array( \'jquery\' ) );
}
add_action( \'wp_enqueue_scripts\', \'searchfunction\' );
css

#ht-masthead .search-field {
  background-color: transparent;
  background-image: url(images/search-icon.png);
  background-position: 5px center;
  background-repeat: no-repeat;
  background-size: 24px 24px;
  border: none;
  cursor: pointer;
  height: 37px;
  margin: 3px 0;
  padding: 0 0 0 34px;
  position: relative;
  -webkit-transition: width 400ms ease, background 400ms ease;
  transition: width 400ms ease, background 400ms ease;
  width: 230px;
}

#ht-masthead .search-field:focus {
  background-color: #fff;
  border: 2px solid #c3c0ab;
  cursor: text;
  outline: 0;
}
#ht-masthead .search-form {
  display: none;
  position: absolute;
  right: 200px;
  top: 200px;
}
.search-toggle:hover #ht-masthead .search-form {
  display: block;
}
.search-form
    .search-submit {
  display: none;
}

.search-form {
  position: relative;
}

.search-form label {
  position: relative;
  background: url(\'https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png\') 0 0 no-repeat;
  background-size: cover;
  width: 50px; height: 50px;
  text-indent: 9999px;
  overflow: hidden;
  white-space: nowrap;
}

.search-input {
  transform: translateY(-100%);
  opacity: 0;
  position: absolute;
  top: 100%;
  transition: opacity .25s, transform .25s;
  left: 0;
  z-index: -1;
  border: 0;
  outline: 0;

}
.search-label, .search-input {
  background: #ccc;
  padding: .5em;
  display: inline-block;
}

.clicked + .search-input {
  opacity: 1;
  transform: translateY(0);
}
页脚。php*

<footer class="site-footer">
    <!-- footer-widgets -->
    <div class="footer-line"></div>
<div class="footer-widgets clearfix">
<?php if (is_active_sidebar(\'footer1\')) : ?>
<div class="footer-widget-area">
<?php dynamic_sidebar(\'footer1\'); ?>
</div>

<?php endif; ?>
<?php if (is_active_sidebar(\'footer2\')) : ?>
<div class="footer-widget-area">
<?php dynamic_sidebar(\'footer2\'); ?>
</div>

<?php endif; ?>

<?php if (is_active_sidebar(\'footer3\')) : ?>
<div class="footer-widget-area">
<?php dynamic_sidebar(\'footer3\'); ?>
</div>

<?php endif; ?>
<?php if (is_active_sidebar(\'footer4\')) : ?>
<div class="footer-widget-area">
<?php dynamic_sidebar(\'footer4\'); ?>
</div>

<?php endif; ?>
</div><!-- /footer-widgets -->
         <nav class="site-nav">
                <?php 

             $args = array(
                \'theme_location\' => \'footer\'
             );

              ?>

             <?php wp_nav_menu( $args ); ?>

            </nav>

<div style="display:inline-block;vertical-align:top;"><p id="copyright">&copy; <?php echo date(\'Y\');?> <?php bloginfo(\'name\'); ?> | </div>
<div style="display:inline-block;vertical-align:top;"><?wp_nav_menu( array( \'theme_location\' => \'new-menu\', \'container_class\' => \'secondary-footer\' ) ); ?></p></div>

</footer>

</footer>

</div><!-- container -->

<?php wp_footer(); ?>
</body>
</html>

1 个回复
最合适的回答,由SO网友:DigitalDesigner 整理而成

我认为这可能是你实现它的方式。看起来您正试图将其作为单个文件添加到主题中,但是我在您提供的代码中没有看到您调用searchform的任何地方。主题本身中的php文件。

因此,我测试了您提供的代码,并通过调整您提供的代码片段的放置位置,成功地将其用于我的自定义主题。

这是我的步骤。

首先,我放置了searchform。php代码插入到标头中。我的主题的php文件。

 <div class="search-field">
 </div>
 <form role="search" method="get" class="search-form" action="<?php echo home_url( \'/\' ); ?>">
   <label for="search-input" id="search-label" class="search-label">
     Search for:   
     </label>
 <input id="search-input" type="search" class="search-input" placeholder="Search …" value="" name="s" title="Search for:" />
   <input type="submit" class="search-submit" value="Search" />
 </form>
接下来,我放置了搜索。js插入页脚。通过在主题周围添加脚本标记并将其添加到结束标记之前,来创建主题的php文件。

 <script>
 document.getElementById("search-label").addEventListener("click", function(e) {
   if (e.target == this) {
     e.preventDefault();
     this.classList.toggle("clicked");
   }
 });
 </script>
之后,我将您的函数添加到函数中。自定义主题的php

 function searchfunction() {
 wp_enqueue_script( \'my-script\', get_stylesheet_directory_uri() . \'/js/search.js\', array( \'jquery\' ) );
 }
 add_action( \'wp_enqueue_scripts\', \'searchfunction\' );
最后,我将您的css样式添加到主题样式表中。

 #ht-masthead .search-field {
 background-color: transparent;
 background-image: url(images/search-icon.png);
 background-position: 5px center;
 background-repeat: no-repeat;
 background-size: 24px 24px;
 border: none;
 cursor: pointer;
 height: 37px;
 margin: 3px 0;
 padding: 0 0 0 34px;
 position: relative;
 -webkit-transition: width 400ms ease, background 400ms ease;
 transition: width 400ms ease, background 400ms ease;
 width: 230px;
 }

 #ht-masthead .search-field:focus {
 background-color: #fff;
 border: 2px solid #c3c0ab;
 cursor: text;
 outline: 0;
 }
 #ht-masthead .search-form {
 display: none;
 position: absolute;
 right: 200px;
 top: 200px;
 }
 .search-toggle:hover #ht-masthead .search-form {
 display: block;
 }
 .search-form
 .search-submit {
 display: none;
 }

 .search-form {
 position: relative;
 }

 .search-form label {
 position: relative;
 background: url(\'https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png\') 0 0 no-repeat;
 background-size: cover;
 width: 50px; height: 50px;
 text-indent: 9999px;
 overflow: hidden;
 white-space: nowrap;
 }

 .search-input {
 transform: translateY(-100%);
 opacity: 0;
 position: absolute;
 top: 100%;
 transition: opacity .25s, transform .25s;
 left: 0;
 z-index: -1;
 border: 0;
 outline: 0;

 }
 .search-label, .search-input {
 background: #ccc;
 padding: .5em;
 display: inline-block;
 }

 .clicked + .search-input {
 opacity: 1;
 transform: translateY(0);
 }
加载页面时,我可以看到与您提供的JSFIDLE示例相同的功能。我确实注意到了我的主题,因为我的标题使用了一个高数字z索引,它在最初打开时隐藏在标题后面。但是,它正在正确加载到站点上。

试试这个解决方案,我希望它能满足您的需要。

结束

相关推荐

Use Tags to initiate Search

首先,我是Wordpress和PHP的新手,如果这是一个基本问题,我深表歉意。我安装了Wordpress,每篇文章都用标签标记,这些标签包含以下链接:href=domain.com/index.php/tag/TAGNAME 但我希望链接是:href=domain.com/index.php/search/TAGNAME 据我所知,我需要改变get_tag_link() 但所做的就是使用get_term_link() 然后我完全迷路了。是否有一个函数If(tag){ return \"