如何在wordpress中仅搜索帖子和页面标题?

时间:2017-07-27 09:53:14

标签: php css wordpress search

我想只按页面搜索并发布标题。可能吗? 我在头文件中的搜索代码在

之下
<form method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="search" class="form-control" placeholder="<?php esc_attr_e( 'Search...', 'consulting' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" /></div>
    <button type="submit" style="height:7.5vh;"><i class="fa fa-search"></i></button>
</form>

3 个答案:

答案 0 :(得分:0)

您可以通过在 functions.php 中添加以下代码来搜索帖子和页面标题:

function __search_by_title_only( $search, &$wp_query )
{
   global $wpdb;
   if(empty($search)) {
      return $search; // skip processing - no search term in query
   }
   $q = $wp_query->query_vars;
   $n = !empty($q['exact']) ? '' : '%';
   $search =  ''; // this must be empty first
   $searchand = '';
   foreach ((array)$q['search_terms'] as $term) {
       $term = esc_sql($wpdb->esc_like($term));
       $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
       $searchand = ' AND ';
   }
   if (!empty($search)) {
      $search = " AND ({$search}) ";
      if (!is_user_logged_in())
         $search .= " AND ($wpdb->posts.post_password = '') ";
   }
   return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

答案 1 :(得分:0)

var clock = new THREE.Clock();
var speed = 2; //units a second
var delta = 0;

render();
function render(){
  requestAnimationFrame(render);

  delta = clock.getDelta();
  object.position.z += speed * delta;

  renderer.render(scene, camera);
}

在主题functions.php文件中添加此代码

答案 2 :(得分:0)

search.php中的

使用以下查询来覆盖默认搜索功能,或者您可以使用自定义模板

$args = array(
            'post_type' => 'post',
            's' => $keyword,
            'cat' => $catSearchID,
            'tag'   => $tag-slug,
            'posts_per_page' => -1,
            'location' => $post_location_search ,
        );

$wp_query = new WP_Query($args);