WordPress를 사용하여 TV 시리즈 데이터베이스를 만들고 싶습니다. 몇 가지 자습서를 따라 두 개의 사용자 지정 게시물 유형이 있습니다. 하나는 for movies, 하나는 for 및 series입니다. 나는 구조를 위해이 포스트를 따랐다 .
내 질문은 : 영화와 시리즈 게시물 유형 사이의 관계를 어떻게 만들 수 있습니까?
"implement the post_type as directed"무슨 뜻인가요?
WordPress를 사용하여 TV 시리즈 데이터베이스를 만들고 싶습니다. 몇 가지 자습서를 따라 두 개의 사용자 지정 게시물 유형이 있습니다. 하나는 for movies, 하나는 for 및 series입니다. 나는 구조를 위해이 포스트를 따랐다 .
내 질문은 : 영화와 시리즈 게시물 유형 사이의 관계를 어떻게 만들 수 있습니까?
"implement the post_type as directed"무슨 뜻인가요?
답변:
관계를위한 아주 좋은 플러그인들 :
메타 박스를 사용하여 간단한 관계를 구축 할 수 있습니다.
add_action( 'admin_init', 'add_meta_boxes' );
function add_meta_boxes() {
add_meta_box( 'some_metabox', 'Movies Relationship', 'movies_field', 'series' );
}
function movies_field() {
global $post;
$selected_movies = get_post_meta( $post->ID, '_movies', true );
$all_movies = get_posts( array(
'post_type' => 'movies',
'numberposts' => -1,
'orderby' => 'post_title',
'order' => 'ASC'
) );
?>
<input type="hidden" name="movies_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
<table class="form-table">
<tr valign="top"><th scope="row">
<label for="movies">Movies</label></th>
<td><select multiple name="movies">
<?php foreach ( $all_movies as $movie ) : ?>
<option value="<?php echo $movie->ID; ?>"<?php echo (in_array( $movie->ID, $selected_movies )) ? ' selected="selected"' : ''; ?>><?php echo $movie->post_title; ?></option>
<?php endforeach; ?>
</select></td></tr>
</table>
}
add_action( 'save_post', 'save_movie_field' );
function save_movie_field( $post_id ) {
// only run this for series
if ( 'series' != get_post_type( $post_id ) )
return $post_id;
// verify nonce
if ( empty( $_POST['movies_nonce'] ) || !wp_verify_nonce( $_POST['movies_nonce'], basename( __FILE__ ) ) )
return $post_id;
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// save
update_post_meta( $post_id, '_movies', array_map( 'intval', $_POST['movies'] ) );
}
그런 다음 영화 관계를 시리즈 게시물의 목록으로 가져옵니다.
$series = new WP_Query( array(
'post_type' => 'movies',
'post__in' => get_post_meta( $series_id, '_movies', true ),
'nopaging' => true
) );
if ( $series-> have_posts() ) { while ( $series->have_posts() ) {
$series->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ></a></li>
<?php
} }
http://domain.com/series-name/movie-name?
나는 추천한다 방금 사용하기 시작한 Posts 2 Posts 플러그인을 .
게시물과 페이지 유형간에 다 대다 관계를 만들 수 있으므로 링크 할 수 있습니다. movies 에 series, 당신이 만들 수있는 다른 CPTS.
이 플러그인을 사용하면 연결 메타 데이터 를 생성하여 연결을 생성 할 때 세부 정보를 얻을 수 있습니다. 사용법이 매우 유연하여 관리자 메타 박스, 연결 유형 및 프런트 엔드에 연결을 표시하는 방법을 제어 할 수 있습니다. 마지막으로 잘 문서화되어 있습니다.