http://ja.forums.wordpress.org/topic/4760?replies=5(カスタム投稿タイプの登録と表示)
こんにちは、ネットワークでカスタム投稿タイプを学んでいます。
タクソノミの使い方が解らないので教えてください。
以下のようなカスタム投稿を、bookやnote等として複数作成した場面で、
add_action('init', 'my_custom_init');
function my_custom_init()
{
$labels= array(
'name'=> _x('Books', 'post type general name'),
'singular_name'=> _x('Book', 'post type singular name'),
'add_new'=> _x('Add New', 'book'),
'add_new_item'=> __('Add New Book'),
'edit_item'=> __('Edit Book'),
'new_item'=> __('New Book'),
'view_item'=> __('View Book'),
'search_items'=> __('Search Books'),
'not_found'=> __('No books found'),
'not_found_in_trash'=> __('No books found in Trash'),
'parent_item_colon'=> ''
);
$args= array(
'labels'=> $labels,
'public'=> true,
'publicly_queryable'=> true,
'show_ui'=> true,
'query_var'=> true,
'rewrite'=> true,
'capability_type'=> 'post',
'hierarchical'=> false,
'menu_position'=> null,
'supports'=> array('title','editor','author','thumbnail','excerpt','comments'),
'taxonomies'=> array('category','post_tag')
);
register_post_type('book',$args);
}
‘taxonomies’=> array(‘category’,’post_tag’)をそれぞれのカスタムポストに書くと、
カテゴリとタグが管理画面のそれぞれのカスタム投稿に表示できるのですが、
実際にブログに表示しようとすると、通常の投稿のカテゴリしか表示することができません。(単に表示方法がわからないだけかもしれませんが、、、)
カテゴリやタグ 、タクソノミは、それぞれのカスタム投稿ごとに独自に設定するものなのか、それとも、カスタムポスト間で共有可能なものなのでしょうか、教えていただけるとうれしいです。
よろしくお願いします。
kz
メンバー
1 week前の投稿 #
タクソノミー(ビルトインのカテゴリー,タグ含む)は全投稿タイプに対して共通です。
例)カスタム投稿タイプbook の カテゴリー も
カスタム投稿タイプnote の カテゴリー も
同じタクソノミー「カテゴリー」です。
実際にブログに表示しようとすると、通常の投稿のカテゴリしか
例えば query_posts( ‘cat=123’ ); として WordPress ループ(while( have_posts() : the_post(); 〜 endwhile;)で記事一覧を表示させてもビルトインの「投稿」しか出てこないのは、デフォルトで ‘post_type’=> ‘post’ と指定されているからなんですー。
WP3.0.1 の時点ではカスタム投稿タイプの記事を扱う場合は、各自で都度 post_type= book 的な指定をしてあげる必要があります。
※ききたい事とちょっとちゃうよーって場合は、
実際にブログに表示しようとすると、通常の投稿のカテゴリしか
を具体的に「こうしたいけどこのコードではこうなってまうでかんて」と書いていただくとより的確なアドバイスが各方面からあると思います!
nobita
メンバー
1 week前の投稿 #
kzさん 教えていただきありがとうございます。
理解できました。
以下のコードで何とか表示できた気分になっています。
勘違いでしたら、教えてください。
<?php
/* loop-category.php */
$posttype_extend = get_post_types( array( 'public' => true,
'_builtin'=> false ) );
$posttype_native = get_post_types( array( 'public' => true,
'_builtin'=> true ) );
$names = array_merge($posttype_extend,$posttype_native);
$count = 0;
$cat_title = single_cat_title('', false);
$cat_id = get_cat_ID($cat_title);
$loop= new WP_Query( array( 'post_type'=> $names,
'posts_per_page'=> $count,'category__and'=> array($cat_id) ) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="entry-title">
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="<?php
printf( esc_attr__( 'Permalink to %s', 'twentyten' ),
the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_title(); ?></a>
</h2>
</div><!-- .entry-title -->
<div class="entry-utility">
<?php twentyten_posted_on(); ?>
<span class="meta-sep"> |</span>
<span class="comments-link"><?php
comments_popup_link( __( 'Leave a comment', 'twentyten' ),
__( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) );
?></span>
<?php
edit_post_link( __( 'Edit', 'twentyten' ),
'<span class="meta-sep"> |</span>
<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-utility -->
<div class="entry-utility">
<?php if ( count( get_the_category() ) ) : ?>
<span class="cat-links"><?php
printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ),
'entry-utility-prep entry-utility-prep-cat-links',
get_the_category_list( ', ' ) ); ?></span>
<span class="meta-sep"> |</span>
<?php endif; ?>
<?php
$tags_list= get_the_tag_list( '', ', ' );
if ( $tags_list ):
?>
<span class="tag-links">
<?php
printf( __( '<span class="%1$s">Tagged</span> %2$s',
'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links',
$tags_list ); ?>
</span>
<span class="meta-sep"> |</span>
<?php endif; ?>
<span class="comments-link">
<?php
comments_popup_link( __( 'Leave a comment', 'twentyten' ),
__( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
</span>
</div><!-- .entry-utility -->
<?php endwhile;?>
kz
メンバー
1 week前の投稿 #
OK◎
■補足
・ループの後に wp_reset_query(); があると安心。
・new WP_Query() ご利用部分は query_posts() でも OK。
nobita
メンバー
1 week前の投稿 #
kzさん
ありがとうございました