여러 사용자 정의 사용자 분류법을 단일 URL에 결합
저는 사용자를 위한 맞춤형 사용자 분류법을 만들었고 단일 분류법에 유용하게 사용됩니다.
Bellow 참조 가이드에서 사용자 정의 사용자 분류법을 만들었습니다.
참조 가이드:- http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress
벨로우는 분류학의 구조입니다.
(registered taxonomy) profession
Dietitian (under the profession )
(registered taxonomy) city
NewYork(under the city)
나의 속칭expert
여기서 나는 어떤 사용자인지 필터 결과를 표시하고 싶습니다.Dietitian에NewYorkcity.so 위의 옵션을 선택한 모든 사용자를 프로필에 표시합니다.
이제 나는 URL과 같은 것을 원합니다.www.test.com/expert/dietitian/newyork사용자 프로필에서 Dietian, New York을 선택한 사용자와 함께.그리고 우리가 함께 할 수 있는 해결책이 있습니까?Dietitian,NewYork
단일 용어가 포함된 URL은 다음과 같습니다.www.test.com/expert/dietitian/
저는 당신이 단지 교착 상태에 대한 튜토리얼에 많은 시간을 할애했다고 생각하기 때문에, 저는 다중 분류법 필터링 및 프런트 엔드에 표시하는 데 필요한 부분에만 참여할 것입니다.
긴 텍스트를 읽기 전에, http://playground.georgemanousarides.com/ 에서 맛을 볼 수 있습니다.
그래서, 첫번째가 우선입니다.논리적 기준은 단일 분류법과 다중 분류법 모두에 대해 동일합니다.분류법과 용어를 등록하고 wp-admin이 필요한 모든 작업을 완료한 후, 우리는 다음이 필요합니다.
방문자가 분류법 쿼리를 선택할 수 있는 페이지(TAX-QUERY PAGE라고 함).
TAX-QUERY PAGE 결과가 표시되는 다른 페이지(TAX-RESULTS PAGE).
뛰어들어요.
단일 분류법
세금 조회 페이지
자습서에 따라 분류학 링크를 표시하는 페이지는 기본 형식에서 다음과 같이 표시됩니다.
방문자는 제공된 링크를 클릭하기만 하면 원하는 분류법을 "선택"할 수 있습니다.
참고:
다음 링크:
- 직업은 다음과 같습니다. www.example.com/user/profession/developer/
- 도시는 다음과 같습니다. www.example.com/user/city/gotham/
슬러그 "사용자/도시" 및 "사용자/전문가"는 함수에서 정의됩니다: my_register_user_taxonomy >> rewrite >> 슬러그
세금-결과 페이지
위에서 언급한 링크를 클릭하면 동일한 분류법 용어로 모든 사용자를 표시하는 페이지(물론 사용자가 정의해야 함)로 이동합니다.
이 작업은 사용자 정의 분류법 템플릿을 만들어 수행합니다.
taxonomy-profession-developer.php 파일을 생성하여 단일 분류법 및 용어 또는 taxonomy-professional을 처리할 수 있습니다.php는 단일 분류법과 그 모든 용어 또는 분류법을 처리합니다.모든 분류법 및 해당 용어에 대한 php.
사용자가 수동으로 작성한 템플릿 파일로 서버가 범람하지 않도록 하려면 분류법을 사용해야 합니다.php 그리고 당신이 자동으로 표시하고 싶은 결과를 설명하는 모든 분류법을 위한 일반적인 테플레이트를 만듭니다.
참고:
사용자를 반복하고 원하는 분류법 용어에 해당하는 사용자만 가져오려면 분류법에서 사용자 지정 쿼리가 필요합니다.php 파일), 튜토리얼에서 언급하고 설명한 대로.
워드프레스가 분류법을 찾고 파일 분류법을 가져올 수 있도록 permalinks는 wp-admin >> settings >> permalinks의 Post Name 옵션에 있어야 합니다.php 및 분류법에서 사용할 수 있는 선택한 분류법에 대한 유용한 정보를 제공합니다.php 파일.
다중 분류법
세금 조회 페이지
방문객들이 맞춤형 분류법에서 용어를 선택할 수 있는 페이지를 만들어야 합니다.이 페이지는 링크(변수 가져오기**)를 통해 TAX-RESULTS 페이지(taxonomy.php)에 필요한 용어를 제공하여 쿼리를 작성합니다.
**TAX-RESULTS 페이지에서 예쁜 영구 링크를 얻으려면 다음과 같은 재작성 기능(여기 참조)을 기능에 추가해야 합니다.php, wp-admin에서 permalinks를 새로 고칩니다.
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'user/(profession|city)/(.+?)/(profession|city)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
'user/(profession|city)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
참고:
만약 당신이 더 많은 분류법을 등록한다면, 함수의 "profession|city"는 "profession|city|tax1|tax2|tax3"가 되어야 합니다.
TAX-QUERY 페이지는 다음과 같습니다.
HTML
<div id="taxonomies">
<h1>Find your professional</h1>
<form>
<?php if ( $taxonomies = get_object_taxonomies( 'user' ) ) : //get all taxonomies under the object_type "user" ( The second parameter given to the function my_register_user_taxonomy of the tutorial )
foreach ( $taxonomies as $taxonomy ) : ?>
<p>
<ul>
<fieldset id="<?php echo esc_attr( $taxonomy ); ?>"> <!-- group the form by taxonomies -->
<legend><h2>Choose <?php echo $taxonomy; ?></h2></legend>
<?php if ( $terms = get_terms( $taxonomy ) ) : //get taxonomy's terms
foreach ( $terms as $term ) : ?>
<li><input type="checkbox" value="<?php echo esc_attr( $term -> slug ); ?>"> <?php echo $term -> name; ?></li>
<?php endforeach;
endif; ?>
</fieldset>
</ul>
</p>
<?php endforeach;
endif; ?>
</form>
<a id="multiTaxSubmit" href="<?php echo esc_attr( get_home_url() . '/user' ); ?>">SUBMIT</a> <!-- this link is processed by jQuery to provide the taxonomy.php with the proper values. The href attribute is the base url needed by the taxonomy.php -->
</div>
JQUERY (외부 파일에 넣을 수 있음)
$( document ).ready( function() {
$( '#multiTaxSubmit' ).click( function ( event ){
event.preventDefault(); //prevent default link url from loading
var taxQuerySubmit = $( this ),
hasChecked = 0;
querySlug = taxQuerySubmit.attr( 'href' ); //get multitax url base from link href attr
$( '#taxonomies fieldset' ).each( function() { //iterate each taxonomy
var checkedTerms = $( this ).find( 'input:checked' ),
checkedLength = checkedTerms.length; //how many terms has the user selected
if ( checkedLength ) {
hasChecked += checkedLength;
querySlug += '/' + $( this ).attr( 'id' ) + '/'; //add taxonomy slug to query url
checkedTerms.each( function( index, value ) {
var comma = ( index == checkedLength-1 ? '' : ',' );
querySlug += $( this ).val() + comma;
} );
}
} );
if ( hasChecked ) {
window.location = querySlug;
} else {
alert( 'Please enter some criteria.' );
}
} );
} );
세금-결과 페이지(taxonomy.php)
<?php
$USERS_BY_TAX = array();
if ( $taxonomies = get_object_taxonomies( 'user' ) ) { //get all taxonomies under the object_type "user" ( The second parameter given to the function my_register_user_taxonomy of the tutorial )
foreach ( $taxonomies as $tax_key => $taxonomy ) {
eval( '$check = $' . $taxonomy . ';' ); // Check if the taxonomy exists in the url. eval outputs $check = $profession, $check = $city etc.
if ( !$check ){
unset( $taxonomies[ $tax_key ] );
continue;
}
eval( '$term_names = explode( ",", $' . $taxonomy . ' );' ); // get terms array from $$taxonomy which gives $profession,$city, the values of which are given through the url as such: $profession="designer,developer"
$USERS_BY_TAX[ $taxonomy ] = array();
foreach ( $term_names as $term_name ) {
$term_obj = get_term_by( 'name', $term_name, $taxonomy ); //get term object for each given term
$users_in_term = get_objects_in_term( $term_obj -> term_id, $taxonomy ); // find users with term
if ( !empty( $users_in_term ) ) {
$USERS_BY_TAX[ $taxonomy ] = $USERS_BY_TAX[ $taxonomy ] + array_fill_keys( $users_in_term, $term_name ) ;
}
}
}
}
/* $USERS_BY_TAX array has all the users for each taxonomy but we only need those who exist in all taxonomies */
if ( $taxonomies ) {
$RESULTS = $USERS_BY_TAX; // keep the initiate array intact
$matched = array_pop( $USERS_BY_TAX ); // first array to compare
$TAXS = $taxonomies;
array_pop( $taxonomies );
if ( !empty( $USERS_BY_TAX ) ) {
foreach ( $taxonomies as $taxonomy ) {
if ( !empty( $USERS_BY_TAX ) ) $matched = array_intersect_key( $matched, $USERS_BY_TAX[ $taxonomy ] );
}
}
}
?>
/* DISPLAY */
<?php if ( $matched ) :
foreach ( array_keys( $matched ) as $user_id ): ?>
<div class="user-entry">
<?php echo get_avatar( get_the_author_meta( 'email', $user_id ), '96' ); ?>
<h2><?php the_author_meta( 'display_name', $user_id ); ?></h2>
<?php if ( in_array( 'profession', $TAXS ) ) : ?><h3>Profession: <?php echo $RESULTS[ 'profession' ][ $user_id ]; ?></h3><?php endif;?>
<?php if ( in_array( 'city', $TAXS ) ) : ?><h3>City: <?php echo $RESULTS[ 'city' ][ $user_id ]; ?></h3><?php endif;?>
<?php echo wpautop( get_the_author_meta( 'description', $user_id ) ); ?>
</div>
<?php endforeach;
else: ?>
<div class="user-entry">
<h2>We are sorry. No results match your criteria.</h2>
<h3>Please <a href="javascript:history.back()">go back</a> and search again!</h3>
</div>
<?php endif; ?>
이 재작성 규칙은 다음과 같이 작동해야 합니다("profession" 및 "city"가 분류법에 등록된 이름이라고 가정).
코드:
function custom_rewrite_rules() {
add_rewrite_rule('^profession/(.*)/city/(.*)?', 'index.php?profession=$matches[1]&city=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rules');
이 코드를 사이트에 저장한 후 다시 쓰기 규칙을 플러시해야 합니다.
URL: http://yourdomain.com/profession/dietitian/city/newyork/
영링크플하거또규면다작시려성을 해야 합니다.flush_rewrite_rules()기능.
<?php
function custom_rewrite_rules() {
flush_rewrite_rules();
add_rewrite_rule( '^products/([^/]*)/([^/]*)/(\d*)?',
'index.php?product_type=$matches[1]&product_brand=$matches[2]&p=$matches[3]',
'top' );
//Post Type: products
//Taxonomy: product_type
//Taxonomy: product_brand
}
add_action('init', 'custom_rewrite_rules');
?>
언급URL : https://stackoverflow.com/questions/34351477/combine-multiple-custom-user-taxonomy-in-single-url
'source' 카테고리의 다른 글
| 오라클에서 커밋되지 않은 읽기 옵션을 허용합니까? (0) | 2023.06.26 |
|---|---|
| 스프링 부트 테스트 - 여러 테스트가 단일 컨텍스트를 공유할 수 있습니까? (0) | 2023.06.26 |
| repo를 새 .gitignore 파일과 다시 동기화합니다. (0) | 2023.06.26 |
| "의존성 분석"에 머무르는 코코파드 (0) | 2023.06.26 |
| 스프링카프카를 사용하는 카프카에 대한 데드 레터 큐(DLQ) (0) | 2023.06.26 |