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

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

一、Divi自定义搜索结果:

  1. // Divi 搜索模块中的产品和其他帖子类型
  2. function custom_remove_default_et_pb_custom_search() {
  3. remove_action( 'pre_get_posts', 'et_pb_custom_search' );
  4. add_action( 'pre_get_posts', 'custom_et_pb_custom_search' );
  5. }
  6. add_action( 'wp_loaded', 'custom_remove_default_et_pb_custom_search' );
  7.  
  8. function custom_et_pb_custom_search( $query = false ) {
  9. if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
  10. return;
  11. }
  12.  
  13. if ( isset( $_GET['et_pb_searchform_submit'] ) ) {
  14. $postTypes = array();
  15.  
  16. /* BEGIN Add custom post types */
  17. $postTypes[] = 'project';
  18. /* END Add custom post types */
  19.  
  20. $query->set( 'post_type', $postTypes );
  21.  
  22. if ( ! empty( $_GET['et_pb_search_cat'] ) ) {
  23. $categories_array = explode( ',', $_GET['et_pb_search_cat'] );
  24. $query->set( 'category__not_in', $categories_array );
  25. }
  26.  
  27. if ( isset( $_GET['et-posts-count'] ) ) {
  28. $query->set( 'posts_per_page', (int) $_GET['et-posts-count'] );
  29. }
  30. }
  31. }

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

添加搜索多个类型:

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

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

Divi Builder

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

2023-8-21 22:57:18

Divi Builder

自动调整 Divi 菜单下拉宽度

2024-10-20 11:41:59

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索
51La