나는 오래 전에 아주 비슷한 것을했고, 나는 속임수로 그것을했다.
내장 된 다시 쓰기 규칙이 너무 복잡하거나 작업을 수행 할 수없는 경우 요청을 잡아 결과를 필터링하는 것이 더 쉽습니다. 단순화 된 버전 :
add_action('parse_request', 'my_parse_request');
function my_parse_request (&$wp) {
$path = $wp->request;
$groups = array();
if (preg_match("%shop/product/([a-zA-Z0-9-]+)%", $path, $groups)) {
$code = $groups[1];
$product = get_product($code); // your own code here
if (isset($product)) {
add_filter('the_posts', 'my_product_filter_posts');
}
}
}
function my_product_filter_posts ($posts) {
ob_start();
echo "stuff goes here"; // your body here
$content = ob_get_contents();
ob_end_clean();
return array(new DummyResult(0, "Product name", $content));
}
설명하려면 :
parse_request
의 조치는 데이터베이스 조회하기 전에 호출된다. URL을 기반으로 다른 작업과 필터를 설치합니다.
게시물의 필터는 데이터베이스 조회 결과를 가짜 결과로 바꿉니다.
class DummyResult {
public $ID;
public $post_title;
public $post_content;
public $post_author;
public $comment_status = "closed";
public $post_status = "publish";
public $ping_status = "closed";
public $post_type = "page";
public $post_date = "";
function __construct ($ID, $title, $content) {
$this->ID = $ID;
$this->post_title = $title;
$this->post_content = $content;
$this->post_author = get_default_author(); // implement this function
}
}
에서 독자 떠났다 많은 숙제가있다 :
DummyResult은 단순한 게시물과 같은 필드가 클래스 또는 그것으로 도망에 불과 충분하다 위와 같지만,보기 흉한 작업 방식입니다. template_redirect
에 대한 필터를 추가하여 일반 페이지 템플릿을 제품 별 템플릿으로 대체하고자 할 수 있습니다. 예쁜 퍼머 링크를 원한다면 URL 정규 표현식을 조정해야 할 수도 있습니다.