검색 모듈에서 상품 타입 검색되게 만들기

검색 모듈에서 상품 타입 검색되게 만들기

Divi 검색 모듈은 기본적으로 표준 게시물 유형(게시물 및 페이지)과 함께 작동합니다. 제품과 같은 다른 유형이 검색되게 하려면 PHP 코드를 추가해야 합니다.

가장 먼저 Divi 업데이트 후에도 변경 사항이 유지되도록 Divi 자식 테마를 생성해야 합니다. 다음은 따라할 수 있는 자식 테마를 만드는 방법에 대한 매뉴얼입니다.

자식 테마를 만든 후에는 다음 코드를 자식 테마의 /functions.php 파일 맨 아래에 배치합니다.

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();
        
		if ( ! isset($_GET['et_pb_include_posts'] ) && ! isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'post' );
        }

		if ( isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'page' );
        }

		if ( isset( $_GET['et_pb_include_posts'] ) ) {
            $postTypes[] = 'post';
        } 

		/* BEGIN Add custom post types */
		$postTypes[] = 'product';
		/* 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'] );
		}
	}
}

다른 유형을 검색 결과에 포함해야 하는 경우 코드의 이 부분을 편집할 수 있습니다.

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

예를 들어 프로젝트 유형을 포함하려면 다음과 같이 코드를 변경합니다.

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

팁: 사용자 정의 유형의 슬러그는 일반적으로 해당 유형의 게시물을 열 때 URL에서, 또는 보기 버튼 위로 마우스를 이동할 때 관리자 화면에서 찾을 수 있습니다.

슬러그

원문: https://intercom.help/elegantthemes/en/articles/3030197-how-to-enable-products-and-other-post-types-in-divi-s-search-module

코멘트 제출

Don`t copy text!