WordPress Divi自定义搜索类型,产品,文章,项目

WordPress自定义搜索结果,默认搜索显示所有,比如:文章,产品,项目,页面等。特殊情况下需要自定义搜索结果,只显示产品或项目。

一、Divi自定义搜索结果:

// Divi 搜索模块中的产品和其他帖子类型
function custom_remove_default_et_pb_custom_search() {
  remove_action( 'pre_get_posts', 'et_pb_custom_search' );
  add_action( 'pre_get_posts', 'custom_et_pb_custom_search' );
}
add_action( 'wp_loaded', 'custom_remove_default_et_pb_custom_search' );

function custom_et_pb_custom_search( $query = false ) {
  if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
    return;
  }

  if ( isset( $_GET['et_pb_searchform_submit'] ) ) {
    $postTypes = array();

    /* BEGIN Add custom post types */
    $postTypes[] = 'project';
    /* END Add custom post types */

    $query->set( 'post_type', $postTypes );

    if ( ! empty( $_GET['et_pb_search_cat'] ) ) {
      $categories_array = explode( ',', $_GET['et_pb_search_cat'] );
      $query->set( 'category__not_in', $categories_array );
    }

    if ( isset( $_GET['et-posts-count'] ) ) {
      $query->set( 'posts_per_page', (int) $_GET['et-posts-count'] );
    }
  }
}

$postTypes[] = ‘project’;,修改project,自定义类型,比如产品:product

添加搜索多个类型:

/* BEGIN Add custom post types */
$postTypes[] = 'product';
$postTypes[] = 'project';
/* END Add custom post types */
WordPress Divi自定义搜索类型,产品,文章,项目

WordPress Divi自定义搜索类型,产品,文章,项目

Divi Builder

Divi主题菜单模块手机端折叠

2023-8-21 22:57:18

VPS技巧

bash: wget: command not found 解决方法

2017-9-10 21:22:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索